java实现【有三个线程ID分别是A、B、C,请有多线编程实现,在屏幕上循环打印10次ABC.】

该题应属于生产者消费者模式一类

 

生产者消费者模式:根据标志位来发消息,实现对线程的控制。

 

直接贴答案了,请各位大虾们指正哦。

 

创建三个线程 如下:ThreadA、ThreadB、ThreadC

 

public class ThreadA implements Runnable {

 Printer printer;

 ThreadA(Printer printer) {
  this.printer = printer;
 }

 public void run() {
  for (int i = 0; i < 10; i++) {
   printer.printThreadA();
  }
 }

}

 

控制打印的类:

 

public class Printer {

 int flag = 0;

 public synchronized void printThreadA() {
  if (flag == 0) {
   System.out.print("A");
   flag = 1;
   notifyAll();
  }
  try {
   wait();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public synchronized void printThreadB() {
  if (flag == 1) {
   System.out.print("B");
   flag = 2;
   notifyAll();
  }
  try {
   wait();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 public synchronized void printThreadC() {
  if (flag == 2) {
   System.out.print("C");
   flag = 0;
   notifyAll();
  }
  try {
   wait();
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

 

main方法:

 

 public static void main(String[] args) {

  Printer printer = new Printer();
  ThreadA ta = new ThreadA(printer);
  ThreadB tb = new ThreadB(printer);
  ThreadC tc = new ThreadC(printer);

  Thread t = new Thread(ta);
  t.start();
  Thread tt = new Thread(tb);
  tt.start();
  Thread ttt = new Thread(tc);
  ttt.start();
 }

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用线程的 wait() 和 notify() 方法来实现按顺序打印ABC的需求。具体实现如下: ```java public class PrintABC { private int times; // 打印数 private int state; // 当前状态:0-A,1-B,2-C public PrintABC(int times) { this.times = times; this.state = 0; } public synchronized void printA() throws InterruptedException { for (int i = 0; i < times; i++) { while (state != 0) { wait(); } System.out.print("A"); state = 1; notifyAll(); } } public synchronized void printB() throws InterruptedException { for (int i = 0; i < times; i++) { while (state != 1) { wait(); } System.out.print("B"); state = 2; notifyAll(); } } public synchronized void printC() throws InterruptedException { for (int i = 0; i < times; i++) { while (state != 2) { wait(); } System.out.print("C"); state = 0; notifyAll(); } } } ``` 在主函数中创建三个线程,分别调用 PrintABC 类中的 printA()、printB() 和 printC() 方法: ```java public static void main(String[] args) { PrintABC printABC = new PrintABC(10); Thread threadA = new Thread(() -> { try { printABC.printA(); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread threadB = new Thread(() -> { try { printABC.printB(); } catch (InterruptedException e) { e.printStackTrace(); } }); Thread threadC = new Thread(() -> { try { printABC.printC(); } catch (InterruptedException e) { e.printStackTrace(); } }); threadA.start(); threadB.start(); threadC.start(); } ``` 执行结果: ```java ABCABCABCABCABCABCABCABCABCABC ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值