Java多線程實(shí)戰(zhàn)之交叉打印的兩種方法
要求效果:先打印5次“printA…”,再打印5次“printB…”,每次打印間隔1秒,重復(fù)循環(huán)20次
方式一:使用wait()和notifyAll()方法
public class MyService {
private volatile boolean flag = false;
public synchronized void printA() {
try {
while (flag) {
wait();
}
for (int i = 0; i < 5; i++) {
System.out.println("printA...");
TimeUnit.SECONDS.sleep(1);
}
flag = true;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public synchronized void printB() {
try {
while (!flag) {
wait();
}
for (int i = 0; i < 5; i++) {
System.out.println("printB...");
TimeUnit.SECONDS.sleep(1);
}
flag = false;
notifyAll();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class BackupA implements Runnable {
private MyService myService;
public BackupA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.printA();
}
}
public class BackupB implements Runnable {
private MyService myService;
public BackupB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.printB();
}
}
public class Run {
public static void main(String[] args) {
MyService myService = new MyService();
for (int i = 0; i < 20; i++) {
new Thread(new BackupA(myService)).start();
new Thread(new BackupB(myService)).start();
}
}
}
方式二:使用await()和signalAll()方法
public class MyService {
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
private boolean flag = false;
public void printA() {
try {
lock.lock();
while (flag) {
condition.await();
}
for (int i = 0; i < 5; i++) {
System.out.println("printA...");
TimeUnit.SECONDS.sleep(1);
}
flag = true;
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printB() {
try {
lock.lock();
while (!flag) {
condition.await();
}
for (int i = 0; i < 5; i++) {
System.out.println("printB...");
TimeUnit.SECONDS.sleep(1);
}
flag = false;
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
public class ThreadA implements Runnable {
private MyService myService;
public ThreadA(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.printA();
}
}
public class ThreadB implements Runnable {
private MyService myService;
public ThreadB(MyService myService) {
super();
this.myService = myService;
}
@Override
public void run() {
myService.printB();
}
}
public class Run {
public static void main(String[] args) {
MyService myService = new MyService();
for (int i = 0; i < 20; i++) {
new Thread(new ThreadA(myService)).start();
new Thread(new ThreadB(myService)).start();
}
}
}
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請(qǐng)查看下面相關(guān)鏈接
相關(guān)文章
java 定時(shí)器線程池(ScheduledThreadPoolExecutor)的實(shí)現(xiàn)
這篇文章主要介紹了java 定時(shí)器線程池(ScheduledThreadPoolExecutor),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Spring BeanPostProcessor(后置處理器)的用法
這篇文章主要介紹了Spring BeanPostProcessor(后置處理器)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
IntelliJ IDEA(2019)之mybatis反向生成的實(shí)現(xiàn)
這篇文章主要介紹了IntelliJ IDEA(2019)之mybatis反向生成,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
解決idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException的問題
這篇文章主要介紹了idea啟動(dòng)報(bào)錯(cuò)javax.imageio.IIOException,解決打不開idea問題,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
Java獲取Prometheus監(jiān)控?cái)?shù)據(jù)的方法實(shí)現(xiàn)
本文主要介紹了Java獲取Prometheus監(jiān)控?cái)?shù)據(jù)的方法實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12
Java動(dòng)態(tài)顯示文件上傳進(jìn)度實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了Java動(dòng)態(tài)顯示文件上傳進(jìn)度實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01

