java中怎么交替打印两个线程,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联-专业网站定制、快速模板网站建设、高性价比中牟网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式中牟网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖中牟地区。费用合理售后完善,10余年实体公司更值得信赖。
使用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<Character> linkedC = new LinkedTransferQueue<Character>(); private static LinkedTransferQueue<Character> linkedN = new LinkedTransferQueue<Character>(); public static void main(String[] args) { String c = "ABCDEFGHI"; char[] ca = c.toCharArray(); String n = "123456789"; char[] na = n.toCharArray(); Thread t1 = new Thread(() -> { for(char caa : ca) { try { linkedC.put(caa); char a = linkedN.take(); System.out.print(a); } catch (InterruptedException e) { e.printStackTrace(); } } }); Thread t2 = new Thread(() -> { for(char naa : na) { try { char b = linkedC.take(); System.out.print(b); linkedN.put(naa); } catch (InterruptedException e) { e.printStackTrace(); } } }); t1.start(); t2.start(); }}
输出结果:
使用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中怎么交替打印两个线程问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
新闻标题:java中怎么交替打印两个线程
文章起源:https://www.cdcxhl.com/article10/piejdo.html
成都网站建设公司_创新互联,为您提供小程序开发、品牌网站建设、网站营销、全网营销推广、域名注册、Google
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联