查询网站是否做301,廊坊网站推广公司,小米网站开发语言,西双版纳傣族自治州勐海县控制三个线程按顺序交替输出数字123123…
synchronized#xff08;配合专用锁对象#xff09;
通过共享锁和 volatile 变量控制执行顺序#xff0c;每个线程按指定顺序打印指定内容#xff0c;确保输出序列如 “123123…”。使用 synchronized 和 wait/notifyAll 实现线程间…控制三个线程按顺序交替输出数字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) {// 创建第一个线程负责输出1Thread t1 new Thread(() - {runTask(1, 2, 1);});t1.start();// 创建第二个线程负责输出2Thread t2 new Thread(() - {runTask(2, 3, 2);});t2.start();// 创建第三个线程负责输出3Thread 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();}Overridepublic 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();}}}
}