1、wait notify 版
public class SyncWaitNotify {//打印标识:1打印a,2打印b,3打印cprivate int flag;//每个字母的打印次数private int loopNumber;//构造方法public SyncWaitNotify(int flag, int loopNumber) {this.flag = flag;this.loopNumber = loopNumber;}public static void main(String[] args) {SyncWaitNotify syncWaitNotify = new SyncWaitNotify(1, 5);//打印a的线程new Thread(() -> {//打印asyncWaitNotify.print(1, 2, "a");}).start();//打印b的线程new Thread(() -> {//打印bsyncWaitNotify.print(2, 3, "b");}).start();//打印c的线程new Thread(() -> {//打印csyncWaitNotify.print(3, 1, "c");}).start();}//根据参数打印不同的字母/** waitFlag:根据这个参数打印对应的字母* nextFlag:下一个要打印的字母对应的flag值* str:要打印的字母* */public void print(int waitFlag, int nextFlag, String str) {for (int i = 0; i < loopNumber; i++) {//加锁synchronized (this) {//判断当前flag值是否和要打印的字母对应的flag值相等while (this.flag != waitFlag) {//不想等则让出cpu,等待其他线程执行try {this.wait();} catch (InterruptedException e) {e.printStackTrace();}}//相等则打印System.out.print(str);//修改flag的值flag = nextFlag;//唤醒所有等待的线程this.notifyAll();}}}
}
2、Lock 条件变量版
public class AwaitSignal extends ReentrantLock {// 循环次数private int loopNumber;public AwaitSignal(int loopNumber) {this.loopNumber = loopNumber;}public void start(Condition first) {this.lock();try {//唤醒打印a的线程first.signal();}finally {this.unlock();}}public void print(String str, Condition current, Condition next) {for (int i = 0; i < loopNumber; i++) {//获得锁this.lock();try {//进入等待current.await();//打印字母System.out.println(str);//唤醒下一个要打印的线程next.signal();}catch (InterruptedException e) {e.printStackTrace();}finally {//释放锁this.unlock();}}}public static void main(String[] args) {AwaitSignal as = new AwaitSignal(5);//a的条件变量Condition aWaitSet = as.newCondition();//b的条件变量Condition bWaitSet = as.newCondition();//c的条件变量Condition cWaitSet = as.newCondition();//打印a的线程new Thread(() -> {as.print("a", aWaitSet, bWaitSet);}).start();//打印b的线程new Thread(() -> {as.print("b", bWaitSet, cWaitSet);}).start();//打印c的线程new Thread(() -> {as.print("c", cWaitSet, aWaitSet);}).start();//先让a线程获取锁,并唤醒a线程,否则所有线程都会进入等待as.start(aWaitSet);}
}