java多線程

生產(chǎn)者與消費者問題

package xianPack;

//生產(chǎn)者    消費者
public class Test10 {

    public static void main(String[] args) {
        
        final Apple apple = new Apple();
        
        //吃蘋果的人      (上下兩種方法一樣)
        new Thread(new Runnable() {
            public void run() {
                try {
                    apple.eat();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        },"線程1").start();
        
        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                try {
                    apple.eat();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        },"線程2");
        thread1.start();
        
        //種蘋果的
        new Thread(new Runnable() {
            public void run() {
                try {
                    apple.plant();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        },"線程3").start();
        
        new Thread(new Runnable() {
            public void run() {
                try {
                    apple.plant();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"線程4").start();
        
    }
}

class Apple {
    
    int num = 0;   //蘋果的數(shù)量
    
    //吃蘋果
    void eat() throws InterruptedException {
        while (true) {
            synchronized (this) {
                if (num > 0) {
                    num--;
                    Thread thread = Thread.currentThread();
                    System.out.println(thread.getName() + "吃蘋果,剩下" + num);
                    //并且通知種蘋果的種
                    notifyAll(); 
                }else {   //沒有蘋果了
                    //進(jìn)入等待狀態(tài)
                    wait();
                    //并且通知種蘋果的種
                    //notify();    //通知一個
                }
            }
            
        }
    }
    
    //種蘋果
    void plant() throws InterruptedException {
        while (true) {
            //鎖放在里邊  一邊吃一邊種
            synchronized (this) {
                //如果生產(chǎn)大于20個,停止生產(chǎn)
                if (num < 20) { 
                    this.num++;
                    Thread thread = Thread.currentThread();
                    System.err.println(thread.getName() + "種蘋果,剩下" + num);
                    
                    //通知吃蘋果的吃
                    notifyAll();    //通知所有的
                }else {     //蘋果太多  不能生產(chǎn)了
                    //等待
                    wait();
                }
            }
            
        }
    }
    
    
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容