Thread 有的时候会程序退出成功,有的时候会无限循环

爱自习的暴鲤龙:
我这个写了两个线程之间转账。一个线程观察

爱自习的暴鲤龙:
有的时候能够结束退出,有的时候就是一直在Ob无限循环了。。。有兴趣帮帮忙看看的嘛。啥原因。。

public class TestThread {
    public static void main(String[] args) {

        Count aCount =new Count(100) ;
        Count bCount =new Count(100) ;
        final Count cCount =new Count(0) ;
        long l = System.currentTimeMillis();


        Thread a= new Thread(new Runnable() {
            @Override
            public void run() {
                while (aCount.total > 0) {
                    synchronized (cCount){
                        aCount.reduce();
                        try {
                            cCount.add();
                            System.out.println(Thread.currentThread().getName()+"-"+aCount.trade+":\t aCount:"+aCount.total+"\t cCount:"+cCount.total);
                            cCount.notify();
                            cCount.wait();//让处锁
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        cCount.notify();
                    }
                }
            }
        },"Thread A");
        Thread b = new Thread(new Runnable() {
            @Override
            public void run() {
                while(bCount.total<200){
                    if (cCount.total>0){
                        synchronized (cCount){
                            try {
                                cCount.reduce();
                                System.out.println(Thread.currentThread().getName()+"-"+bCount.trade+":\t bCount:"+bCount.total+"\t cCount:"+cCount.total);
                                cCount.notify();
                                cCount.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                            bCount.add();
                            cCount.notify();
                        }
                    }
                }
            }
        },"Thread B");

        Thread o = new Thread(()->{
            while(bCount.total<200){
                System.out.println("aCount.total:"+aCount.total+"\t bCount.total:"+bCount.total+"\t cCount.total:"+cCount.total);
            }
        },"Thread O:");
        o.start();
        a.start();
        b.start();

    }

    public static class Count implements AddAndReduce{
        int total;
        int trade;

        public Count(int total){
            this.total = total;
            this.trade= 0;
        }

        @Override
        public void add() {
            this.total +=1;
            this.trade +=1;
        }

        @Override
        public  void reduce() {
            this.total -=1;
            this.trade +=1;

        }
    }


}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值