java实现多线程交替打印两个数
本文实例为大家分享了java实现多线程交替打印两个数的具体代码,供大家参考,具体内容如下
方法1、使用wait和notify
package com.thread; public class T01 { public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); Object object = new Object(); Thread thread1 = new Thread(() -> { synchronized(object){//使用notify和wait时,必须要选获取到锁 for (int i = 0; i < char1.length; i++) { try { System.out.print(char1[i]); object.notify(); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } object.notify();//必须加上,否则程序无法结束,两个线程总有一个最后是wait状态,所以此处必须加 } },"t1"); Thread thread2 = new Thread( () -> { synchronized(object){ for (int i = 0; i < char2.length; i++) { try { System.out.print(char2[i]); object.notify(); object.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } object.notify(); } },"t2"); thread1.start(); thread2.start(); } }
方法2、使用LockSupport方法
package com.thread; import java.util.concurrent.locks.LockSupport; public class T02 { static Thread thread1 ; static Thread thread2 ; public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); thread1 = new Thread(() -> { for (int i = 0; i < char1.length; i++) { System.out.print(char1[i]); LockSupport.unpark(thread2); LockSupport.park(); } },"t1"); thread2 = new Thread(() -> { for (int i = 0; i < char2.length; i++) { LockSupport.park(); System.out.print(char2[i]); LockSupport.unpark(thread1); } },"t2"); thread1.start(); thread2.start(); } }
方法3、使用CAS自旋锁
package com.thread; public class T03 { enum ReadEnum{ T1, T2; } static volatile ReadEnum r = ReadEnum.T1; public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); Thread thread1 = new Thread(() ->{ for (int i = 0; i < char1.length; i++) { while (r != ReadEnum.T1) { } System.out.print(char1[i]); r = ReadEnum.T2; } },"t1"); Thread thread2 = new Thread(() ->{ for (int i = 0; i < char2.length; i++) { while (r != ReadEnum.T2) { } System.out.print(char2[i]); r = ReadEnum.T1; } },"t2"); thread1.start(); thread2.start(); } }
方法4、使用Condition方法
package com.thread; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.ReentrantLock; public class T04 { public static void main(String[] args) { char[] char1 = "AAAAAA".toCharArray(); char[] char2 = "BBBBBB".toCharArray(); ReentrantLock lock = new ReentrantLock(); Condition condition1 = lock.newCondition(); Condition condition2 = lock.newCondition(); Thread thread1 = new Thread(() ->{ try { lock.lock(); for (int i = 0; i < char1.length; i++) { System.out.print(char1[i]); condition2.signal();//唤醒线程2执行 condition1.await();//线程1等待 } condition2.signal(); }catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } },"t1"); Thread thread2 = new Thread(() ->{ try { lock.lock(); for (int i = 0; i < char2.length; i++) { System.out.print(char2[i]); condition1.signal(); condition2.await(); } condition1.signal(); } catch (Exception e) { e.printStackTrace(); }finally{ lock.unlock(); } },"t2"); thread1.start(); thread2.start(); } }
Condition与notify相比的好处是,Condition可以指定需要唤醒的线程,而notify是无法指定的,只能随机唤醒一个或者全唤醒(notifyAll)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
相关文章
IntelliJ IDEA(2019)之mybatis反向生成的实现
这篇文章主要介绍了IntelliJ IDEA(2019)之mybatis反向生成,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2019-10-10@Transaction,@Async在同一个类中注解失效的原因分析及解决
这篇文章主要介绍了@Transaction,@Async在同一个类中注解失效的原因分析及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2021-12-12spring boot validation参数校验与分组嵌套各种类型及使用小结
参数校验基本上是controller必做的事情,毕竟前端传过来的一切都不可信,validation可以简化这一操作,这篇文章主要介绍了spring boot validation参数校验分组嵌套各种类型及使用小结,需要的朋友可以参考下2023-09-09Java线程池ThreadPoolExecutor源码深入分析
ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程调度,线程池管理等等服务2022-08-08
最新评论