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

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条)

报告相同问题?

悬赏问题

  • ¥50 关于#微信小程序#的问题:微信小程序支持h5实现webrtc h264 h265低延迟传输渲染,能付费帮我们解决一下吗
  • ¥20 请问ansys License manager 这个问题如何解决
  • ¥15 memory.limit()' is no longer supported
  • ¥15 基于stm32f4产生两路pwm信号并可由串口通信调节
  • ¥15 C++/QT设置函数调用条件(多个功能调用同一,且各自调用条件不同)
  • ¥15 UE5样条线生成的网格UV问题
  • ¥15 如何用最短的时间大致看懂springboot+vue的项目
  • ¥15 (有偿)懂数值分析和含时变参数微分方程的来
  • ¥15 abaqus随机生成二维颗粒
  • ¥15 安装ansys许可证管理器时出现了这个问题,如何解决?