怎么在Java中利用多线程实现交叉打印?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
方式一:使用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(); } } }
1.SpringMVC,Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架。2.Shiro,Apache Shiro是Java的一个安全框架。3.Mybatis,MyBatis 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。4.Dubbo,Dubbo是一个分布式服务框架。5.Maven,Maven是个项目管理和构建自动化工具。6.RabbitMQ,RabbitMQ是用Erlang实现的一个高并发高可靠AMQP消息队列服务器。7.Ehcache,EhCache 是一个纯Java的进程内缓存框架。
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。