classic problem: producer and consumer

來源:互聯網
上載者:User

注意: 1. wait()後 立即放鎖, 給其他等待線程使用,當被喚醒,立即從wait()處執行(而不是從方法頭重新執行)

          2. notify(), 喚醒其他睡著的線程,不能喚醒自己。

          3. 沒有結束的問題用 while(用if, 則可能在錯誤情況下繼續執行)。

 

public class ProducerConsumer {
    public static void main(String[] args) {
        SyncStack ss = new SyncStack();
       
        Producer p = new Producer(ss);
        Consumer c = new Consumer(ss);
       
        new Thread(c).start();
        new Thread(p).start();
        new Thread(p).start();
        new Thread(p).start();
    }
}

 

class Wotou {
    int id;
   
    Wotou(int id) {
        this.id = id;
    }
   
    public String toString() {
        return "Wotou: " + id;
    }
}

class SyncStack {
    int index = 0;
    //Wotou[] wotou = new Wotou[6];
   
    public synchronized void push(/*Wotou wt,*/ int i) {       
        while (index == 6) {
            System.out.println("----push(Wotou wt):" + index);
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
        }

        this.notify();
        System.out.println(Thread.currentThread().getName() + "-----producer: " + i );
        //wotou[index] = wt;
        index++;
    }
   
    public synchronized int pop(int i) {
        System.out.println("pop(: " + i);
        while (index == 0) {
            System.out.println("pop():" + index);
            try {
                this.wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }           
        }

        System.out.println("pre this.notify()");
        this.notify();
        System.out.println("end this.notify()");
       
        index--;
        System.out.println("Consumer: " + i);
        return index;
        //return wotou[index];
    }
}

class Producer implements Runnable{
    SyncStack ss;
   
    Producer(SyncStack ss) {
        this.ss = ss;
    }
   
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i=0; i<10; i++){
            Wotou wt = new Wotou(i);
            ss.push(/*wt,*/ i);
                       
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ie){
                ie.printStackTrace();
            }
        }   
    }
}

class Consumer implements Runnable{
    SyncStack ss;
   
    Consumer(SyncStack ss) {
        this.ss = ss;
    }
   
    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (int i=0; i<30; i++){
            ss.pop(i);
           
            try {
                Thread.sleep(100);
            } catch (InterruptedException ie){
                ie.printStackTrace();
            }
        }   
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.