Producer-Consumer Pattern
package com.albert.thread;
import java.util.Random;
public class Producer_Consumer {
public static void test()
{
Table table = new Table(3);
new MakerThread("MakerThread-1",table,31415).start();
new MakerThread("MakerThread-2",table,33333).start();
new MakerThread("MakerThread-3",table,55555).start();
new EaterThread("EaterThread-1",table,66666).start();
new EaterThread("EaterThread-2",table,666906).start();
new EaterThread("EaterThread-3",table,26666).start();
}
}
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)
{
wait();
}
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;
}
}
class MakerThread extends Thread
{
private final Random random;
private final Table table;
private static int id = 0;
public MakerThread(String name,Table table,int seed)
{
super(name);
this.table = table;
this.random = new Random(seed);
}
public void run()
{
while(true)
{
try {
Thread.sleep(this.random.nextInt(1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String cake = "[CakeNo."+nextId()+" by "+getName()+"";
try {
table.put(cake);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static synchronized int nextId()
{
return id++;
}
}
class EaterThread extends Thread
{
private final Random random;
private final Table table;
public EaterThread(String name,Table table,int seed)
{
super(name);
this.table = table;
this.random = new Random(seed);
}
public void run()
{
while(true)
{
try {
String cake = table.take();
Thread.sleep(this.random.nextInt(1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
import java.util.Random;
public class Producer_Consumer {
public static void test()
{
Table table = new Table(3);
new MakerThread("MakerThread-1",table,31415).start();
new MakerThread("MakerThread-2",table,33333).start();
new MakerThread("MakerThread-3",table,55555).start();
new EaterThread("EaterThread-1",table,66666).start();
new EaterThread("EaterThread-2",table,666906).start();
new EaterThread("EaterThread-3",table,26666).start();
}
}
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)
{
wait();
}
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;
}
}
class MakerThread extends Thread
{
private final Random random;
private final Table table;
private static int id = 0;
public MakerThread(String name,Table table,int seed)
{
super(name);
this.table = table;
this.random = new Random(seed);
}
public void run()
{
while(true)
{
try {
Thread.sleep(this.random.nextInt(1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String cake = "[CakeNo."+nextId()+" by "+getName()+"";
try {
table.put(cake);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static synchronized int nextId()
{
return id++;
}
}
class EaterThread extends Thread
{
private final Random random;
private final Table table;
public EaterThread(String name,Table table,int seed)
{
super(name);
this.table = table;
this.random = new Random(seed);
}
public void run()
{
while(true)
{
try {
String cake = table.take();
Thread.sleep(this.random.nextInt(1000));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.