浏览量:81 最近编辑于:null
# Java多线程打印ABC
## 使用synchronized
```java
public class AlternatePrintABC {
private static final Object lock = new Object();
private static int state = 0; // 0: 打印A, 1: 打印B, 2: 打印C
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
for (int i = 0; i < 10; i++) { // 每个线程打印10次
synchronized (lock) {
while (state != 0) { // 如果不是A的轮次,等待
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.print("A");
state = 1; // 切换到B
lock.notifyAll(); // 唤醒其他线程
}
}
});
Thread threadB = new Thread(() -> {
for (int i = 0; i < 10; i++) {
synchronized (lock) {
while (state != 1) { // 如果不是B的轮次,等待
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.print("B");
state = 2; // 切换到C
lock.notifyAll(); // 唤醒其他线程
}
}
});
Thread threadC = new Thread(() -> {
for (int i = 0; i < 10; i++) {
synchronized (lock) {
while (state != 2) { // 如果不是C的轮次,等待
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
System.out.print("C");
state = 0; // 切换到A
lock.notifyAll(); // 唤醒其他线程
}
}
});
threadA.start();
threadB.start();
threadC.start();
}
}
```
## 使用ReentrantLock 和 Condition
```java
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class AlternatePrintABCWithLock {
private static final Lock lock = new ReentrantLock();
private static final Condition conditionA = lock.newCondition();
private static final Condition conditionB = lock.newCondition();
private static final Condition conditionC = lock.newCondition();
private static int state = 0; // 0: 打印A, 1: 打印B, 2: 打印C
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
for (int i = 0; i < 10; i++) {
lock.lock();
try {
while (state != 0) {
conditionA.await(); // 等待打印A的信号
}
System.out.print("A");
state = 1; // 切换到B
conditionB.signal(); // 唤醒打印B的线程
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
});
Thread threadB = new Thread(() -> {
for (int i = 0; i < 10; i++) {
lock.lock();
try {
while (state != 1) {
conditionB.await(); // 等待打印B的信号
}
System.out.print("B");
state = 2; // 切换到C
conditionC.signal(); // 唤醒打印C的线程
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
});
Thread threadC = new Thread(() -> {
for (int i = 0; i < 10; i++) {
lock.lock();
try {
while (state != 2) {
conditionC.await(); // 等待打印C的信号
}
System.out.print("C");
state = 0; // 切换到A
conditionA.signal(); // 唤醒打印A的线程
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} finally {
lock.unlock();
}
}
});
threadA.start();
threadB.start();
threadC.start();
}
}
```
## 使用Semaphore
```java
import java.util.concurrent.Semaphore;
public class AlternatePrintABCWithSemaphore {
private static final Semaphore semaphoreA = new Semaphore(1); // A线程先执行
private static final Semaphore semaphoreB = new Semaphore(0); // B线程等待
private static final Semaphore semaphoreC = new Semaphore(0); // C线程等待
public static void main(String[] args) {
Thread threadA = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
semaphoreA.acquire(); // 获取A的信号量
System.out.print("A");
semaphoreB.release(); // 释放B的信号量
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
Thread threadB = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
semaphoreB.acquire(); // 获取B的信号量
System.out.print("B");
semaphoreC.release(); // 释放C的信号量
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
Thread threadC = new Thread(() -> {
for (int i = 0; i < 10; i++) {
try {
semaphoreC.acquire(); // 获取C的信号量
System.out.print("C");
semaphoreA.release(); // 释放A的信号量
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
});
threadA.start();
threadB.start();
threadC.start();
}
}
```
评论