java线程 打印_Java多线程实战之交叉打印的两种方法

要求效果:先打印5次“printA…”,再打印5次“printB…”,每次打印间隔1秒,重复循环20次

方式一:使用wait()和notifyAll()方法

public class MyService {

private volatile boolean flag = false;

public synchronized void printA() {

try {

while (flag) {

wait();

}

for (int i = 0; i < 5; i++) {

System.out.println("printA...");

TimeUnit.SECONDS.sleep(1);

}

flag = true;

notifyAll();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

public synchronized void printB() {

try {

while (!flag) {

wait();

}

for (int i = 0; i < 5; i++) {

System.out.println("printB...");

TimeUnit.SECONDS.sleep(1);

}

flag = false;

notifyAll();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public class BackupA implements Runnable {

private MyService myService;

public BackupA(MyService myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printA();

}

}

public class BackupB implements Runnable {

private MyService myService;

public BackupB(MyService myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printB();

}

}

public class Run {

public static void main(String[] args) {

MyService myService = new MyService();

for (int i = 0; i < 20; i++) {

new Thread(new BackupA(myService)).start();

new Thread(new BackupB(myService)).start();

}

}

}

方式二:使用await()和signalAll()方法

public class MyService {

private Lock lock = new ReentrantLock();

private Condition condition = lock.newCondition();

private boolean flag = false;

public void printA() {

try {

lock.lock();

while (flag) {

condition.await();

}

for (int i = 0; i < 5; i++) {

System.out.println("printA...");

TimeUnit.SECONDS.sleep(1);

}

flag = true;

condition.signalAll();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

public void printB() {

try {

lock.lock();

while (!flag) {

condition.await();

}

for (int i = 0; i < 5; i++) {

System.out.println("printB...");

TimeUnit.SECONDS.sleep(1);

}

flag = false;

condition.signalAll();

} catch (InterruptedException e) {

e.printStackTrace();

} finally {

lock.unlock();

}

}

}

public class ThreadA implements Runnable {

private MyService myService;

public ThreadA(MyService myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printA();

}

}

public class ThreadB implements Runnable {

private MyService myService;

public ThreadB(MyService myService) {

super();

this.myService = myService;

}

@Override

public void run() {

myService.printB();

}

}

public class Run {

public static void main(String[] args) {

MyService myService = new MyService();

for (int i = 0; i < 20; i++) {

new Thread(new ThreadA(myService)).start();

new Thread(new ThreadB(myService)).start();

}

}

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本之家的支持。如果你想了解更多相关内容请查看下面相关链接

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值