java两个线程交替打印_java实现两个线程交替打印

86d2eb1cf4f20c90058a5713d09be90c.png

使用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();

}

}

最后输出结果:

e8f6c7832333c910f6e1976455c9f6f7.png

免费学习视频教程推荐:java教学视频

使用LinkedTransferQueue实现两个线程交替打印

实现字母在前数字在后package com.study.pattern;

import java.util.concurrent.LinkedTransferQueue;

public class Demo3 {

private static LinkedTransferQueue linkedC = new LinkedTransferQueue();

private static LinkedTransferQueue linkedN = new LinkedTransferQueue();

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();

}

}

输出结果:

a4f1d6237dcf8236205d25a5ab825697.png

使用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();

}

}

输出结果:

3933ea92de56858327a58acae2501f0f.png

使用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();

}

}

输出结果:

702eed0e50576214a4aa6df923abe960.png

相关文章教程推荐:java零基础入门

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值