sinat_34458693
2016-03-29 07:10
采纳率: 50%
浏览 1.5k
已采纳

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();
                }
            }
        }
    }
    
    点赞 评论
  • sinat_34458693 2016-03-29 08:17

    class bread {
    int num = 0;

    public synchronized void makeBreand() {
        num++;
        this.notify();
    
    }
    
    public synchronized void sale() {
    
        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("面包数量+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("售出面包一个   目前有" + left.num + "个面包");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    }

    点赞 评论
  • f2boy 2016-03-29 08:22

    这个很显然,你的saleThread线程的run方法没有循环条件,里面的逻辑只会执行一次。
    可能有些时候一次都不执行,这是因为saleThread可能会先于makeThread执行,此时left.num==0,run方法就结束了。

    还有,bread的sale方法里面的while语句很奇怪,这个地方使用while与if有什么区别么?
    还有,为什么类名都是小写的……

    点赞 评论
  • guanjun_12 2017-12-10 03:09

    循环条件不对,应该用结束标志的

    点赞 评论

相关推荐 更多相似问题