[java] 控制三个线程按顺序交替输出数字123123…
·
控制三个线程按顺序交替输出数字123123…
synchronized(配合专用锁对象)
通过共享锁和 volatile 变量控制执行顺序,每个线程按指定顺序打印指定内容,确保输出序列如 “123123…”。使用 synchronized 和 wait/notifyAll 实现线程间协作。
/**
* AlternateOutput类用于实现三个线程交替输出数字的功能
* 通过使用synchronized关键字和wait/notify机制,确保三个线程按照1->2->3的顺序循环输出
*/
public class AlternateOutput {
/**
* 用于线程同步的锁对象
*/
private static final Object lock = new Object();
/**
* 当前应该执行的线程编号
*/
private static int currentThreadNum = 1;
/**
* 每个线程需要执行的循环次数
*/
private static final int MAX_LOOP = 10;
/**
* 线程执行的任务方法,负责按照指定顺序输出内容
* @param curThreadNum 当前线程的编号,用于判断是否轮到当前线程执行
* @param nextThreadNum 下一个应该执行的线程编号,执行完当前任务后更新此值
* @param output 当前线程需要输出的内容
*/
private static void runTask(int curThreadNum, int nextThreadNum, String output) {
// 循环执行指定次数的任务
for (int i = 0; i < AlternateOutput.MAX_LOOP; i++) {
// 使用synchronized块确保线程安全
synchronized (lock) {
// 等待直到轮到当前线程执行
while (AlternateOutput.currentThreadNum != curThreadNum) {
try {
lock.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
// 输出当前线程的内容
System.out.print(output);
// 更新下一个应该执行的线程编号
currentThreadNum = nextThreadNum;
// 唤醒所有等待的线程
lock.notifyAll();
}
}
}
/**
* 程序入口点,创建并启动三个线程实现交替输出
* @param args 命令行参数
*/
public static void main(String[] args) {
// 创建第一个线程,负责输出"1"
Thread t1 = new Thread(() -> {
runTask(1, 2, "1");
});
t1.start();
// 创建第二个线程,负责输出"2"
Thread t2 = new Thread(() -> {
runTask(2, 3, "2");
});
t2.start();
// 创建第三个线程,负责输出"3"
Thread t3 = new Thread(() -> {
runTask(3, 1, "3");
});
t3.start();
// 等待所有线程执行完成
try {
t1.join();
t2.join();
t3.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
}
ReentrantLock和Condition
通过ReentrantLock和Condition控制执行顺序,确保按1→2→3循环打印10次。每个线程等待其编号轮次,执行打印后唤醒下一个线程。
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
public class PrintNumber {
public static final int MAX_LOOP = 10;
public static int currentNum = 1;
public static final ReentrantLock lock = new ReentrantLock();
public static final Condition c1 = lock.newCondition();
public static final Condition c2 = lock.newCondition();
public static final Condition c3 = lock.newCondition();
public static void main(String[] args) {
Thread t1 = new Thread(new PrintNumberTask(1));
Thread t2 = new Thread(new PrintNumberTask(2));
Thread t3 = new Thread(new PrintNumberTask(3));
t1.start();
t2.start();
t3.start();
}
}
import java.util.concurrent.locks.Condition;
public class PrintNumberTask implements Runnable {
private int number;
public PrintNumberTask(int num) {
this.number = num;
}
private void printAndSwith(String output, int nextNum, Condition awaitCondition, Condition signalCondition)
throws InterruptedException {
while (PrintNumber.currentNum != this.number) {
awaitCondition.await();
}
System.out.print(output);
PrintNumber.currentNum = nextNum;
signalCondition.signal();
}
@Override
public void run() {
for (int i = 0; i < PrintNumber.MAX_LOOP; i++) {
PrintNumber.lock.lock();
try {
switch (number) {
case 1:
printAndSwith("1", 2, PrintNumber.c1, PrintNumber.c2);
break;
case 2:
printAndSwith("2", 3, PrintNumber.c2, PrintNumber.c3);
break;
case 3:
printAndSwith("3", 1, PrintNumber.c3, PrintNumber.c1);
break;
default:
break;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
PrintNumber.lock.unlock();
}
}
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐



所有评论(0)