日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

练习题之Wait/Notify

發布時間:2025/4/5 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 练习题之Wait/Notify 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

方案一:

public class PrintABC {public static void main(String[] args) {AtomicInteger synObj = new AtomicInteger(0);Runnable r = new ThreadModel(synObj);new Thread(r,"A").start();new Thread(r,"B").start();new Thread(r,"C").start();} }public class ThreadModel implements Runnable {public volatile AtomicInteger ato;public ThreadModel(AtomicInteger ato) {this.ato = ato;}public void run() {for(int i=0;i<10;i++) {synchronized(ato) {if(ato.get()%3==0) {System.out.println("A);ato.set(ato.get()+1);ato.notifyAll();} else if(ato.get()%3==1) {System.out.println("B);ato.set(ato.get()+1);ato.notifyAll();} else if(ato.get()%3==2) {System.out.println("C);ato.set(ato.get()+1);ato.notifyAll();} else {ato.wait();}}}} }

方案二:

public class printABC {public static void main(String [] args) {AtomicInteger ato = new AtomicInteger(0);ThreadModel modelA = new ThreadModel(ato,"A",0);ThreadModel modelB = new ThreadModel(ato,"B",0);ThreadModel modelC = new ThreadModel(ato,"C",0);new Thread(modelA).start();new Thread(modelB).start();new Thread(modelC).start();}}public class ThreadModel {private AtomicInteger ato;private String printChar;private int flag;private int count = 0;public ThreadModel(AtomicInteger ato,String printChar,int flag) {this.ato = ato;this.printChar = printChar;this.flag = flag;}public void run() {synchronized(ato) {while(true) {if(ato.get()%3==flag) {System.out.println(printChar);ato.set(ato.get()+1);ato.notifyAll();count++;if(count==10) {break;}}else {ato.wait();}}}}}

?

總結

以上是生活随笔為你收集整理的练习题之Wait/Notify的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。