Skip to content

Commit af104ce

Browse files
feat
1 parent 217db7a commit af104ce

File tree

5 files changed

+287
-0
lines changed

5 files changed

+287
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.concurrent.Semaphore;
2+
3+
/**
4+
* @Author: zzStar
5+
* @Date: 2021/9/9
6+
* @Description: 通用解法:交替打印任意
7+
*/
8+
public class InTurnCommonBySemaphore {
9+
10+
// 规定的次数
11+
private static final int times = 10;
12+
13+
// 需要打印任意就任意个信号量,但是第一个必须为1
14+
static Semaphore a = new Semaphore(1);
15+
static Semaphore b = new Semaphore(0);
16+
17+
public static void main(String[] args) {
18+
new Thread(() -> print("a", a, b)).start();
19+
new Thread(() -> print("b", b, a)).start();
20+
}
21+
22+
private static void print(String name, Semaphore cur, Semaphore next) {
23+
for (int i = 0; i < times; i++) {
24+
try {
25+
cur.acquire();
26+
System.out.println(name);
27+
next.release();
28+
} catch (InterruptedException e) {
29+
e.printStackTrace();
30+
}
31+
}
32+
}
33+
34+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import java.util.concurrent.Semaphore;
2+
3+
/**
4+
* @Author: zzStar
5+
* @Date: 2021/9/9
6+
* @Description: N个线程顺序打印0-100,Semaphore
7+
*/
8+
public class print100Example01 {
9+
10+
// 定义N的值
11+
private final static int n = 100;
12+
static int result = 0;
13+
static int maxNum = 100;
14+
15+
public static void main(String[] args) throws InterruptedException {
16+
final Semaphore[] semaphores = new Semaphore[n];
17+
for (int i = 0; i < n; i++) {
18+
// 非公平信号量,每个信号量初始计数都为1
19+
semaphores[i] = new Semaphore(1);
20+
if (i != n - 1) {
21+
// System.out.println(i + "===" + semaphores[i].getQueueLength());
22+
// 获取一个许可前线程将一直阻塞, for 循环之后只有 semaphores[2] 没有被阻塞
23+
semaphores[i].acquire();
24+
}
25+
}
26+
for (int i = 0; i < n; i++) {
27+
// 初次执行,上一个信号量是 semaphores[2]
28+
final Semaphore lastSemaphore = i == 0 ? semaphores[n - 1] : semaphores[i - 1];
29+
final Semaphore currentSemaphore = semaphores[i];
30+
final int index = i;
31+
new Thread(() -> {
32+
try {
33+
while (true) {
34+
// 初次执行,让第一个 for 循环没有阻塞的 semaphores[2] 先获得令牌阻塞了
35+
lastSemaphore.acquire();
36+
System.out.println("thread" + index + ": " + result++);
37+
if (result > maxNum) {
38+
System.exit(0);
39+
}
40+
// 释放当前的信号量,semaphores[0] 信号量此时为 1,下次 for semaphores[0]
41+
currentSemaphore.release();
42+
}
43+
} catch (Exception e) {
44+
e.printStackTrace();
45+
}
46+
}).start();
47+
}
48+
}
49+
50+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.util.concurrent.*;
2+
3+
/**
4+
* @Author: zzStar
5+
* @Date: 2021/9/9
6+
* @Description: 交叉打印ABC,Synchronized + wait/notify
7+
*/
8+
public class printABCExample01 {
9+
10+
private static int counter = 0;
11+
12+
private static final Object OBJ = new Object();
13+
14+
public static void main(String[] args) {
15+
16+
ExecutorService executorService = new ThreadPoolExecutor(
17+
3,
18+
5,
19+
3,
20+
TimeUnit.SECONDS,
21+
new LinkedBlockingDeque<>(3),
22+
Executors.defaultThreadFactory(),
23+
new ThreadPoolExecutor.DiscardOldestPolicy());
24+
25+
executorService.execute(() -> {
26+
int cnt = 0;
27+
while (cnt < 10) {
28+
synchronized (OBJ) {
29+
if (counter % 3 != 0) {
30+
try {
31+
OBJ.wait();
32+
} catch (InterruptedException e) {
33+
e.printStackTrace();
34+
}
35+
} else {
36+
System.out.println("A");
37+
cnt++;
38+
counter++;
39+
}
40+
OBJ.notifyAll();
41+
}
42+
}
43+
});
44+
45+
executorService.execute(() -> {
46+
int cnt = 0;
47+
while (cnt < 10) {
48+
synchronized (OBJ) {
49+
if (counter % 3 != 1) {
50+
try {
51+
OBJ.wait();
52+
} catch (InterruptedException e) {
53+
e.printStackTrace();
54+
}
55+
} else {
56+
System.out.println("B");
57+
cnt++;
58+
counter++;
59+
}
60+
OBJ.notifyAll();
61+
}
62+
}
63+
});
64+
65+
executorService.execute(() -> {
66+
int cnt = 0;
67+
while (cnt < 10) {
68+
synchronized (OBJ) {
69+
if (counter % 3 != 2) {
70+
try {
71+
OBJ.wait();
72+
} catch (InterruptedException e) {
73+
e.printStackTrace();
74+
}
75+
} else {
76+
System.out.println("C");
77+
cnt++;
78+
counter++;
79+
}
80+
OBJ.notifyAll();
81+
}
82+
}
83+
});
84+
85+
executorService.shutdown();
86+
}
87+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import java.util.concurrent.locks.Lock;
2+
import java.util.concurrent.locks.ReentrantLock;
3+
4+
/**
5+
* @Author: zzStar
6+
* @Date: 2021/9/9
7+
* @Description: 交叉打印ABC,ReentrantLock
8+
*/
9+
public class printABCExample02 {
10+
11+
// 通过state的值来确定是哪个线程打印
12+
private static int state = 0;
13+
14+
private final static Lock LOCK = new ReentrantLock();
15+
16+
public static void main(String[] args) {
17+
18+
new Thread(() -> {
19+
for (int i = 0; i < 10; ) {
20+
LOCK.lock();
21+
try {
22+
while (state % 3 == 0) {
23+
System.out.println("A");
24+
state++;
25+
i++;
26+
}
27+
} finally {
28+
LOCK.unlock();
29+
}
30+
}
31+
}).start();
32+
33+
new Thread(() -> {
34+
for (int i = 0; i < 10; ) {
35+
LOCK.lock();
36+
try {
37+
while (state % 3 == 1) {
38+
System.out.println("B");
39+
state++;
40+
i++;
41+
}
42+
} finally {
43+
LOCK.unlock();
44+
}
45+
}
46+
}).start();
47+
48+
new Thread(() -> {
49+
for (int i = 0; i < 10; ) {
50+
LOCK.lock();
51+
try {
52+
while (state % 3 == 2) {
53+
System.out.println("C");
54+
state++;
55+
i++;
56+
}
57+
} finally {
58+
LOCK.unlock();
59+
}
60+
}
61+
}).start();
62+
63+
}
64+
65+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import java.util.concurrent.*;
2+
3+
/**
4+
* @Author: zzStar
5+
* @Date: 2021/9/9
6+
* @Description: 交叉打印ABC,Semaphore
7+
*/
8+
public class printABCExample03 {
9+
10+
private int times = 10;
11+
12+
// 只有A 初始信号量为1,第一次获取到的只能是A
13+
private static Semaphore A = new Semaphore(1);
14+
15+
private static Semaphore B = new Semaphore(0);
16+
17+
private static Semaphore C = new Semaphore(0);
18+
19+
public static void main(String[] args) {
20+
21+
ExecutorService executorService = new ThreadPoolExecutor(
22+
3,
23+
5,
24+
3,
25+
TimeUnit.SECONDS,
26+
new LinkedBlockingDeque<>(3),
27+
Executors.defaultThreadFactory(),
28+
new ThreadPoolExecutor.DiscardOldestPolicy());
29+
30+
printABCExample03 printAbc = new printABCExample03();
31+
executorService.execute(() -> printAbc.print("A", A, B));
32+
executorService.execute(() -> printAbc.print("B", B, C));
33+
executorService.execute(() -> printAbc.print("C", C, A));
34+
35+
36+
executorService.shutdown();
37+
}
38+
39+
private void print(String name, Semaphore current, Semaphore next) {
40+
for (int i = 0; i < times; i++) {
41+
try {
42+
current.acquire();
43+
System.out.println(name);
44+
next.release();
45+
} catch (InterruptedException e) {
46+
e.printStackTrace();
47+
}
48+
}
49+
}
50+
51+
}

0 commit comments

Comments
 (0)