《java多线程设计模式 第五章Producer Consumer》

来源:互联网 发布:淘宝天猫积分怎么获得 编辑:程序博客网 时间:2024/05/16 08:30





/*************************************************************************************************************************************************************************************/

Table类:

public class Table{private final String[] buffer;private int tail;//下一个put的地方private int head;//下一个take的地方private int count;//buffer内的蛋糕数量public Table(int count){this.buffer = new String[count];this.head = 0;this.tail = 0;this.count = 0;}public synchronized void put(String cake) throws InterruptedException{System.out.println(Thread.currentThread().getName()+"puts "+cake);while(count >= buffer.length){System.out.println(Thread.currentThread().getName()+"wait() begin "+cake);wait();System.out.println(Thread.currentThread().getName()+"wait() end "+cake);}buffer[tail] = cake;tail = (tail + 1) % buffer.length;count++;notifyAll();}public synchronized String take() throws InterruptedException{while(count <= 0){wait();}String cake = buffer[head];head = (head + 1)%buffer.length;count--;notifyAll();System.out.println(Thread.currentThread().getName()+"takes "+cake);return cake;}}
/*************************************************************************************************************************************************************************************/
public class MakeThread extends Thread{private final Random random;private final Table table;private static int id = 0;// 蛋糕流水号public MakeThread(String name, Table table, long seed){super(name);this.table = table;this.random = new Random(seed);}public void run(){try{while (true){Thread.sleep(random.nextInt(1000));String cake = "[Cake No. " + nextId() + " by " + getName() + "]";table.put(cake);}}catch (InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();}}private static synchronized int nextId(){return id++;}}
/*************************************************************************************************************************************************************************************/

import java.util.Random;public class EaterThread extends Thread{private final Random random;private final Table table;public EaterThread(String name,Table table,long seed){super(name);this.table = table;this.random = new Random(seed);}public void run(){try        {        while(true)        {        String cake = table.take();        Thread.sleep(random.nextInt(1000));        }        }        catch (InterruptedException e)        {        // TODO: handle exception        }}}

/*************************************************************************************************************************************************************************************/


public class Main{/** * @param args */public static void main(String[] args){Table table = new Table(3);new MakeThread("makerthread 1 ", table, 31415).start();new MakeThread("makerthread 2 ", table, 31415).start();new MakeThread("makerthread 3 ", table, 31415).start();new EaterThread("eaterthread 1 ", table, 32568).start();new EaterThread("eaterthread 2 ", table, 32568).start();new EaterThread("eaterthread 3 ", table, 32568).start();}}

类图结构。


0 0
原创粉丝点击