时间:2021-05-19
使用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实现两个线程交替打印的详细内容,感谢大家的学习和对的支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
使用wait()和notify()实现Java多线程通信:两个线程交替打印A和B,如ABABABpublicclassTest{publicstaticvoid
本文实例为大家分享了java实现多线程交替打印两个数的具体代码,供大家参考,具体内容如下方法1、使用wait和notifypackagecom.thread;p
实现需求:两个线程交替打印1,0,打印10轮java多线程口诀:高内聚,低耦合线程操作资源类判断干活通知防止虚假唤醒方式一:使用synchronized和Obj
本文实例为大家分享了java实现多线程交替打印的具体代码,供大家参考,具体内容如下notify+wait实现importorg.junit.Test;impor
Python中使用threading.Condition交替打印两个字符的程序。这个程序涉及到两个线程的的协调问题,两个线程为了能够相互协调运行,必须持有一个共