时间:2021-05-20
要求效果:先打印5次“printA…”,再打印5次“printB…”,每次打印间隔1秒,重复循环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(); } }}总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Java多线程有序执行的几种方法总结同事无意间提出了这个问题,亲自实践了两种方法。当然肯定还会有更多更好的方法。方法一importjava.util.concu
本文介绍了浅谈Java的两种多线程实现方式,分享给大家。具有如下:一、创建多线程的两种方式Java中,有两种方式可以创建多线程:1通过继承Thread类,重写T
IOS多线程编程NSThread的使用方法NSThread是多线程的一种,有两种方法创建子线程(1)优点:NSThread比GCD、NSOperation都轻量
java多线程的几种实现方法总结1.多线程有几种实现方法?同步有几种实现方法?多线程有两种实现方法,分别是继承Thread类与实现Runnabl
一、java多线程基本入门java多线程编程还是比较重要的,在实际业务开发中经常要遇到这个问题。java多线程,传统创建线程的方式有两种。1、继承自Thread