Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d301eda

Browse files
committed
feat: add implemention Lock and Semaphore
1 parent 71d131f commit d301eda

File tree

8 files changed

+610
-5
lines changed

8 files changed

+610
-5
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.hi.dhl.algorithms.other.concurrency;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* <pre>
8+
* author: dhl
9+
* date : 2021年1月8日
10+
* desc :
11+
* </pre>
12+
*/
13+
14+
class PrintABCLock {
15+
16+
private ReentrantLock lock = new ReentrantLock();
17+
private Condition ca = lock.newCondition();
18+
private Condition cb = lock.newCondition();
19+
private Condition cc = lock.newCondition();
20+
private boolean runa = true;
21+
private boolean runb = false;
22+
private boolean runc = false;
23+
24+
private void printA() {
25+
while (true) {
26+
lock.lock();
27+
try {
28+
while (!runa) {
29+
ca.await();
30+
}
31+
System.out.println("A");
32+
Thread.sleep(1000);
33+
runa = false;
34+
runb = true;
35+
runc = false;
36+
cb.signalAll();
37+
} catch (Exception e) {
38+
39+
} finally {
40+
lock.unlock();
41+
}
42+
}
43+
}
44+
45+
private void printB() {
46+
while (true) {
47+
lock.lock();
48+
try {
49+
while (!runb) {
50+
cb.await();
51+
}
52+
System.out.println("B");
53+
Thread.sleep(1000);
54+
runa = false;
55+
runb = false;
56+
runc = true;
57+
cc.signalAll();
58+
} catch (Exception e) {
59+
60+
} finally {
61+
lock.unlock();
62+
}
63+
}
64+
}
65+
66+
private void printC() {
67+
while (true) {
68+
lock.lock();
69+
try {
70+
while (!runc) {
71+
cc.await();
72+
}
73+
System.out.println("C");
74+
Thread.sleep(1000);
75+
runa = true;
76+
runb = false;
77+
runc = false;
78+
ca.signalAll();
79+
} catch (Exception e) {
80+
81+
} finally {
82+
lock.unlock();
83+
}
84+
}
85+
}
86+
87+
public static void main(String... args) {
88+
PrintABCLock lock = new PrintABCLock();
89+
new Thread(() -> {
90+
lock.printA();
91+
}).start();
92+
93+
new Thread(() -> {
94+
lock.printB();
95+
}).start();
96+
97+
new Thread(() -> {
98+
lock.printC();
99+
}).start();
100+
}
101+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package com.hi.dhl.algorithms.other.concurrency;
2+
3+
/**
4+
* <pre>
5+
* author: dhl
6+
* date : 2021年1月8日
7+
* desc :
8+
* </pre>
9+
*/
10+
11+
class PrintABCSync {
12+
private static final Object LOCK = new Object();
13+
private boolean runA = true;
14+
private boolean runB = false;
15+
private boolean runC = false;
16+
17+
private void printA() {
18+
while (true) {
19+
synchronized (LOCK) {
20+
try {
21+
while (!runA) {
22+
LOCK.wait();
23+
}
24+
System.out.println("A");
25+
Thread.sleep(1000);
26+
runA = false;
27+
runB = true;
28+
runC = false;
29+
LOCK.notifyAll();
30+
} catch (Exception e) {
31+
32+
}
33+
}
34+
}
35+
}
36+
37+
private void printB() {
38+
while (true) {
39+
synchronized (LOCK) {
40+
try {
41+
while (!runB) {
42+
LOCK.wait();
43+
}
44+
System.out.println("B");
45+
Thread.sleep(1000);
46+
runA = false;
47+
runB = false;
48+
runC = true;
49+
LOCK.notifyAll();
50+
} catch (Exception e) {
51+
52+
}
53+
}
54+
}
55+
}
56+
57+
private void printC() {
58+
while (true) {
59+
synchronized (LOCK) {
60+
try {
61+
while (!runC) {
62+
LOCK.wait();
63+
}
64+
65+
System.out.println("C");
66+
Thread.sleep(1000);
67+
runA = true;
68+
runB = false;
69+
runC = false;
70+
LOCK.notifyAll();
71+
} catch (Exception e) {
72+
73+
}
74+
}
75+
}
76+
}
77+
78+
public static void main(String... args) {
79+
PrintABCSync lock = new PrintABCSync();
80+
new Thread(() -> {
81+
lock.printA();
82+
}).start();
83+
84+
new Thread(() -> {
85+
lock.printB();
86+
}).start();
87+
88+
new Thread(() -> {
89+
lock.printC();
90+
}).start();
91+
}
92+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.hi.dhl.algorithms.other.concurrency;
2+
3+
import java.util.concurrent.locks.Condition;
4+
import java.util.concurrent.locks.ReentrantLock;
5+
6+
/**
7+
* <pre>
8+
* author: dhl
9+
* date : 2021年1月8日
10+
* desc :
11+
* </pre>
12+
*/
13+
14+
class PrintNumberLock {
15+
16+
private ReentrantLock lock = new ReentrantLock();
17+
private Condition even = lock.newCondition();
18+
private Condition odd = lock.newCondition();
19+
private boolean runOdd = true;
20+
21+
private void printOdd() {
22+
while (true) {
23+
lock.lock();
24+
try {
25+
while (!runOdd) {
26+
odd.await();
27+
}
28+
29+
System.out.println(1);
30+
Thread.sleep(1000);
31+
runOdd = false;
32+
even.signalAll();
33+
} catch (Exception e) {
34+
35+
} finally {
36+
lock.unlock();
37+
}
38+
}
39+
}
40+
41+
private void printEven() {
42+
while (true) {
43+
lock.lock();
44+
try {
45+
while (runOdd) {
46+
even.await();
47+
}
48+
System.out.println(2);
49+
Thread.sleep(1000);
50+
runOdd = true;
51+
odd.signalAll();
52+
} catch (Exception e) {
53+
54+
} finally {
55+
lock.unlock();
56+
}
57+
}
58+
}
59+
60+
public static void main(String... args) {
61+
PrintNumberLock lock = new PrintNumberLock();
62+
new Thread(() -> {
63+
lock.printEven();
64+
}).start();
65+
66+
new Thread(() -> {
67+
lock.printOdd();
68+
}).start();
69+
}
70+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.hi.dhl.algorithms.other.concurrency;
2+
3+
import java.util.concurrent.Semaphore;
4+
5+
/**
6+
* <pre>
7+
* author: dhl
8+
* date : 2021年1月8日
9+
* desc :
10+
* </pre>
11+
*/
12+
13+
class PrintNumberSemaphore {
14+
private Semaphore semaphoreOdd = new Semaphore(1);
15+
private Semaphore semaphoreEven = new Semaphore(0);
16+
17+
private void printOdd() {
18+
while (true) {
19+
try {
20+
semaphoreOdd.acquire();
21+
System.out.println(1);
22+
Thread.sleep(1000);
23+
semaphoreEven.release();
24+
} catch (Exception e) {
25+
26+
}
27+
}
28+
}
29+
30+
private void printEven() {
31+
while (true) {
32+
try {
33+
semaphoreEven.acquire();
34+
System.out.println(2);
35+
Thread.sleep(1000);
36+
semaphoreOdd.release();
37+
} catch (Exception e) {
38+
39+
}
40+
}
41+
}
42+
43+
public static void main(String... args) {
44+
PrintNumberSemaphore lock = new PrintNumberSemaphore();
45+
new Thread(() -> {
46+
lock.printOdd();
47+
}).start();
48+
49+
new Thread(() -> {
50+
lock.printEven();
51+
}).start();
52+
}
53+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /