sinat_34458693 2016-03-29 07:10 采纳率: 0%
浏览 1519
已采纳

java se 多线程资源共享问题

class bread{
    int num = 0;

    public synchronized void makeBreand(){
            num++;
            this.notify();      

    }

    public synchronized void sale(){
        while(num==0){
            try{
                this.wait();
                System.out.println("暂时无面包,等待");
            }catch(InterruptedException e){e.printStackTrace();}

        }
        num--;

    }
}

class makeThread implements Runnable{
bread account;

public makeThread(bread s){
    account = s;

}
public void run(){
while(account.num<10){
    account.makeBreand();
    System.out.println(Thread.currentThread().getName()+"thread is working");
    System.out.println("面包数量+1,已有"+account.num+"个面包");
    try{
        Thread.sleep(1000);
    }catch(InterruptedException e){e.printStackTrace();}
}
    }

}

class saleThread implements Runnable{
bread left;
public saleThread(bread s){
left = s;
}
public void run(){
if(left.num>0){
left.sale();
System.out.println(Thread.currentThread().getName()+"售出面包一个");
System.out.println("目前有"+left.num+"个面包");
try{
Thread.sleep(1000);
}catch(InterruptedException e){e.printStackTrace();}
}
}
}

public class test{
public static void main(String[] args){
bread b = new bread();

    saleThread sale = new saleThread(b);
    makeThread make = new makeThread(b);
    Thread t1 = new Thread(make);
    Thread t2 = new Thread(sale);
    t1.start();
    t2.start();
}

}

代码如下,运行效果:
Thread-1售出面包一个
Thread-0thread is working
目前有0个面包
面包数量+1,已有0个面包
Thread-0thread is working
面包数量+1,已有1个面包
Thread-0thread is working
面包数量+1,已有2个面包
Thread-0thread is working
面包数量+1,已有3个面包
Thread-0thread is working
面包数量+1,已有4个面包
Thread-0thread is working
面包数量+1,已有5个面包
Thread-0thread is working
面包数量+1,已有6个面包
Thread-0thread is working
面包数量+1,已有7个面包
Thread-0thread is working
面包数量+1,已有8个面包
Thread-0thread is working
面包数量+1,已有9个面包
Thread-0thread is working
面包数量+1,已有10个面包

为什么我的sale进程只被调用了一次?

  • 写回答

4条回答

  • 毕小宝 博客专家认证 2016-03-29 07:45
    关注

    首先,sale操作是在线程1中执行的,因为第一个线程t1 先启动了,导致num很快就非0类,那么sale操作的while循环就结束了。
    其次,你的一个操作中的打印语句应该放在同一条,因为是多线程环境下,比如你的sale操作中前后两条打印语句可能就是错乱的,不是同时出现的。修正下你的打印语句,就知道为什么了。

     class bread {
        int num = 0;
    
        public synchronized void makeBreand() {
            num++;
            this.notify();
    
        }
    
        public synchronized void sale() {
            System.out.println(Thread.currentThread().getName()+" num:"+num);
            while (num == 0) {
                try {
                    System.out.println("暂时无面包,等待");
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
    
            }
            num--;
    
        }
    }
    
    class makeThread implements Runnable {
        bread account;
    
        public makeThread(bread s) {
            account = s;
    
        }
    
        public void run() {
            while (account.num < 10) {
                account.makeBreand();
                System.out.println(Thread.currentThread().getName() + "thread is working"+"面包数量+1,已有" + account.num + "个面包");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    
    }
    
    class saleThread implements Runnable {
        bread left;
    
        public saleThread(bread s) {
            left = s;
        }
    
        public void run() {
            if (left.num > 0) {
                left.sale();
                System.out.println(Thread.currentThread().getName() + "售出面包一个"+"目前有" + left.num + "个面包");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog