Java-Thread-生產者消費模式

來源:互聯網
上載者:User

標籤:list   tac   not   eth   wait   remove   gen   ted   stat   

package com.cn.thread;

import java.util.ArrayList;
import java.util.List;
/**
 * 類比 生產者消費模式
 * @author johnzhang
 *
 */
public class WaitAndNotifyMain {
    public static void main(String[] args) {
        WaitAndNotify waitAndNotify = new WaitAndNotify();
        Runnable r1 = new Thread1(waitAndNotify);
        Runnable r2 = new Thread2(waitAndNotify);
        new Thread(r2).start();
        new Thread(r1).start();
        
    }
}

class Thread1 implements Runnable {
    WaitAndNotify waitAndNotify;

    public Thread1(WaitAndNotify waitAndNotify) {
        this.waitAndNotify = waitAndNotify;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        waitAndNotify.getList();
    }

}

class Thread2 implements Runnable {
    WaitAndNotify waitAndNotify;

    public Thread2(WaitAndNotify waitAndNotify) {
        this.waitAndNotify = waitAndNotify;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        waitAndNotify.setList();
    }

}

class WaitAndNotify {
    private int size;
    private List<String> context;
    private boolean isok = false;

    public WaitAndNotify() {
        this.size = 10;
        context = new ArrayList<String>();
//        for (int i = 0; i < size; i++) {
//            context.add("值" + (i + 1));
//        }
    }

    public synchronized void setList() {
        try {
            while (true) {
                if(isok){
                    System.out.println(" 做 好啦擷取" + isok);
                    this.wait();
                }else{
                    for (int i = 1; i <= size; i++) {
                        System.out.println("要製作啦啦,製作的東西是:食物" + i );
                        context.add("食物" + i);
                        Thread.sleep(500);
                    }
                    isok = true;
                    this.notify();
                }
                
            }
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
    }

    public synchronized void getList() {
        try {
            while (true) {
                if(!isok){
                    System.out.println("沒有 食物 禁止使用者 吃" + isok);
                    this.wait();
                }else{
                    System.out.println("有 食物 使用者 吃" + isok);
                    for (int i = 1; i <= size; i++) {
                        System.out.println("要消費啦,消費的東西是" + context.get(0) + " I = " + i +" Context.size = " + context.size());
                        System.out.println("Context.value" + context.toString());
                        context.remove(0);//每次吃掉第一個
                        Thread.sleep(500);
//                        System.out.println("睡眠後返回 i=============== " +  i);
                    }
                    isok = false;
                    this.notify();
                }
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
    }
}

Java-Thread-生產者消費模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.