多线程练习2

使用三个线程使得ABC 循环输出十次

package com.bonc;


public class ThreadTest {

   private boolean isA = true;
   private boolean isB = false;
   private boolean isC = false;
    private int count = 0;
   class Print{
       public synchronized void printA(){
           while(isA){
               System.out.print("A");
               isA=false;
               isB=true;
               isC=false;
               notifyAll();
           }
           while (!isA){
               try {
                   wait();
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
       }

       public synchronized void printB(){
           while (isB){
               System.out.print("B");
               isA=false;
               isB = false;
               isC=true;
               notifyAll();
           }
           while (!isB){
//               if (count>=10) break;
               try {
                   wait();
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
       }

       private synchronized void printC(){
           while (isC){
               System.out.println("C");
               count++;
//               System.out.println(count);
               isA=true;
               isB=false;
               isC=false;
               notifyAll();
           }
           while (!isC){
               if (count>=10) break;
               try {
                   wait();
               }catch (Exception e){
                   e.printStackTrace();
               }
           }
       }
   }

   class PrintThreadA implements Runnable{
       private Print p;
       public PrintThreadA(Print p){
           this.p=p;
       }

       @Override
       public void run() {
           for (int i = 0; i < 10; i++) {
               p.printA();
           }
       }
   }

    class PrintThreadB implements Runnable{
        private Print p;
        public PrintThreadB(Print p){
            this.p=p;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                p.printB();
            }
        }
    }

    class PrintThreadC implements Runnable{
        private Print p;
        public PrintThreadC(Print p){
            this.p=p;
        }

        @Override
        public void run() {
            for (int i = 0; i < 10; i++) {
                p.printC();
//                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) {
        ThreadTest threadTest = new ThreadTest();
        Print print = threadTest.new Print();
        PrintThreadA printThreadA = threadTest.new PrintThreadA(print);
        PrintThreadB printThreadB = threadTest.new PrintThreadB(print);
        PrintThreadC printThreadC = threadTest.new PrintThreadC(print);
        new Thread(printThreadA).start();
        new Thread(printThreadB).start();
        new Thread(printThreadC).start();
    }

}
运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值