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

报告相同问题?

悬赏问题

  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作