多线程顺序打印问题


三个线程分别打印 A,B,C

三个线程分别打印 A,B,C,要求这三个线程一起运行,打印 n 次,输出形如“ABCABCABC….”的字符串

使用Lock

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.concurrent.locks.ReentrantLock;

public class ABC {
static class ABCPrinter {
private final int times; // 需要打印的次数
private volatile int state; // 存储状态值,打印完成时该值会变成3*time-1
private final ReentrantLock lock;

public ABCPrinter(int times) {
this.times = times;
this.lock = new ReentrantLock();
}

public void print(Character key, int num) {
int current = 0;
while (current < times) {
lock.lock();
if (state % 3 == num) { // 通过取模判断当前该哪个线程进入打印
System.out.println(key);
state++;
current++;
}
lock.unlock(); // 打印完成或非当前线程轮次会进入解锁
}
}
}

public static void main(String[] args) throws InterruptedException {
final ABCPrinter abcPrinter = new ABCPrinter(10);
Thread a = new Thread(() -> abcPrinter.print('A', 0));
a.start();
Thread b = new Thread(() -> abcPrinter.print('B', 1));
b.start();
Thread c = new Thread(() -> abcPrinter.print('C', 2));
c.start();
a.join();
b.join();
c.join();
}
}

使用wait/notify

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class ABC {
static class ABCPrinter {
private final static Object LOCK = new Object();
private final int times; // 需要打印的次数
private volatile int state; // 存储状态值,打印完成时该值会变成3*time-1

public ABCPrinter(int times) {
this.times = times;
}

public void print(Character key, int num) {
int current = 0;
while (current < times) {
synchronized(LOCK){
while (state % 3 != num) { // 存在虚假唤醒的问题,必须设置为while循环
try {
LOCK.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
state++;
current++;
System.out.println(key);
LOCK.notifyAll(); // notifyAll和wait方法必须放在synchronized中,不让编译器会抛出IllegalMonitorStateException
}
}
}
}

public static void main(String[] args) throws InterruptedException {
final ABCPrinter abcPrinter = new ABCPrinter(10);
Thread a = new Thread(() -> abcPrinter.print('A', 0));
a.start();
Thread b = new Thread(() -> abcPrinter.print('B', 1));
b.start();
Thread c = new Thread(() -> abcPrinter.print('C', 2));
c.start();
a.join();
b.join();
c.join();
}
}

两个线程交替打印0~100的奇偶数

两个线程交替打印0~100的奇偶数

使用wait/notify

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
public class OddEvenPrinter {

static class Printer {

private final static Object LOCK = new Object();
private volatile int state = 0;
private final int times;

public Printer(int times) {
this.times = times;
}

public void print(int mod) {
while (state < times) {
synchronized (LOCK) {
while (state % 2 != mod) { // 处理虚假唤醒
try {
LOCK.wait();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
System.out.println("name: " + Thread.currentThread().getName() + ", number: " + state++);
LOCK.notify();
}
}
}

}

public static void main(String[] args) {
Printer printer = new Printer(100);
new Thread(() -> printer.print(0)).start();
new Thread(() -> printer.print(1)).start();
}

}

通过N个线程顺序循环打印从0至100

通过N个线程顺序循环打印从0至100

使用Semaphore

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.concurrent.Semaphore;

public class NThreadPrinter {
private final int limit = 100; // 打印的最大值
private final int threadCount; // 线程数
private volatile int state = 0; // 共享状态变量
private final Semaphore[] semaphores; // 每个线程对应的信号量

public NThreadPrinter(int threadCount) {
this.threadCount = threadCount;
semaphores = new Semaphore[threadCount];
// 初始化信号量,除了第一个线程信号量为 1,其它为 0
for (int i = 0; i < threadCount; i++) {
semaphores[i] = new Semaphore(i == 0 ? 1 : 0);
}
}

public void print(int threadId) {
while (true) {
try {
semaphores[threadId].acquire(); // 获取当前线程的信号量
if (state > limit) {
semaphores[(threadId + 1) % threadCount].release();
return;
} // 打印完成,退出
System.out.println("线程 " + threadId + " 打印:" + state++);
// 唤醒下一个线程
semaphores[(threadId + 1) % threadCount].release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

public static void main(String[] args) {
int N = 3; // 线程数量
NThreadPrinter printer = new NThreadPrinter(N);

// 启动 N 个线程
for (int i = 0; i < N; i++) {
final int threadId = i;
new Thread(() -> printer.print(threadId)).start();
}
}
}

作者

jszero

发布于

2025-03-08

更新于

2025-04-20

许可协议

评论