重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
java中怎么交替打印两个线程,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
成都创新互联公司服务项目包括瑶海网站建设、瑶海网站制作、瑶海网页制作以及瑶海网络营销策划等。多年来,我们专注于互联网行业,利用自身积累的技术优势、行业经验、深度合作伙伴关系等,向广大中小型企业、政府机构等提供互联网行业的解决方案,瑶海网站推广取得了明显的社会效益与经济效益。目前,我们服务的客户以成都为中心已经辐射到瑶海省份的部分城市,未来相信会继续扩大服务区域并继续获得客户的支持与信任!使用ReentrantLock实现两个线程交替打印
实现字母在前数字在后
package com.study.pattern; import java.util.concurrent.CountDownLatch;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock; public class Demo2 { private static Lock lock = new ReentrantLock(); private static Condition c1 = lock.newCondition(); private static Condition c2 = lock.newCondition(); private static CountDownLatch count = new CountDownLatch(1); public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); Thread t1 = new Thread(() -> { try { lock.lock(); count.countDown(); for(char caa : ca) { c1.signal(); System.out.print(caa); c2.await(); } c1.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }); Thread t2 = new Thread(() -> { try { count.await(); lock.lock(); for(char naa : na) { c2.signal(); System.out.print(naa); c1.await(); } c2.signal(); } catch (InterruptedException e) { e.printStackTrace(); } finally { lock.unlock(); } }); t1.start(); t2.start(); }}
最后输出结果:
使用LinkedTransferQueue实现两个线程交替打印
实现字母在前数字在后
package com.study.pattern; import java.util.concurrent.LinkedTransferQueue; public class Demo3 { private static LinkedTransferQueue
输出结果:
使用synchronized实现两个线程交替打印
实现字母在前数字在后
package com.study.pattern; import java.util.concurrent.CountDownLatch; public class Demo4 { private static CountDownLatch count = new CountDownLatch(1); public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); Object lock = new Object(); Thread t1 = new Thread(() -> { synchronized (lock) { count.countDown(); for(char caa : ca) { System.out.print(caa); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }); Thread t2 = new Thread(() -> { try { count.await(); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock) { for(char naa : na) { System.out.print(naa); lock.notify(); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } lock.notify(); } }); t1.start(); t2.start(); }}
输出结果:
使用LockSupport实现两个线程交替打印
实现字母在前数字在后
package com.study.pattern; import java.util.concurrent.locks.LockSupport; public class Demo5 { private static Thread t1; private static Thread t2; public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); t1 = new Thread(() -> { for(char caa : ca) { System.out.print(caa); LockSupport.unpark(t2); LockSupport.park(); } }); t2 = new Thread(() -> { for(char naa : na) { LockSupport.park(); System.out.print(naa); LockSupport.unpark(t1); } }); t1.start(); t2.start(); }}
关于java中怎么交替打印两个线程问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。