评论

收藏

[Java] Java多线程实战之交叉打印的两种方法

编程语言 编程语言 发布于:2021-09-18 19:38 | 阅读数:549 | 评论:0

今天小编就为大家分享一篇关于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();
 }
 }
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对CodeAE代码之家的支持。如果你想了解更多相关内容请查看下面相关链接
原文链接:https://blog.csdn.net/qq_40378034/article/details/86766001

关注下面的标签,发现更多相似文章