您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
这篇文章主要介绍了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<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怎么实现两个线程交替打印”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。