生产者-消费者


package debug;
import java.util.regex.*;
import java.util.*;


class Food{}

class Table extends LinkedList{
int maxSize;
public Table(int maxSize){
this.maxSize = maxSize;
}
public synchronized void putFood(Food f){
while(this.size() >= this.maxSize){
try{
this.wait();
}catch(Exception e){}
}
this.add(f);
notifyAll();
}

public synchronized Food getFood(){
while(this.size() <= 0){
try{
this.wait();
}catch(Exception e){}
}
Food f = (Food)this.removeFirst();
notifyAll();
return f;
}
}


class Chef extends Thread{
Table t;
String name;
Random r = new Random(12345);
public Chef(String name,Table t){
this.t = t;
this.name = name;
}
public void run(){
while(true){
Food f = make();
System.out.println(name+" put a Food:"+f);
t.putFood(f);
}
}
private Food make(){
try{
Thread.sleep(200+r.nextInt(200));
}catch(Exception e){}
return new Food();
}
}

class Eater extends Thread{
Table t;
String name;
Random r = new Random(54321);
public Eater(String name,Table t){
this.t = t;
this.name = name;
}
public void run(){
while(true){
Food f = t.getFood();
System.out.println(name+" get a Food:"+f);
eat(f);

}
}
private void eat(Food f){

try{
Thread.sleep(400+r.nextInt(200));
}catch(Exception e){}
}
}

public class Test {
public static void main(String[] args) throws Exception{
Table t = new Table(10);
new Chef("Chef1",t).start();
new Chef("Chef2",t).start();
new Chef("Chef3",t).start();
new Chef("Chef4",t).start();
new Eater("Eater1",t).start();
new Eater("Eater2",t).start();
new Eater("Eater3",t).start();
new Eater("Eater4",t).start();
new Eater("Eater5",t).start();
new Eater("Eater6",t).start();

}
}




class Producer implements Runnable {

private final BlockingQueue queue;

Producer(BlockingQueue q) { queue = q; }

public void run() {

try {

while(true) { queue.put(produce()); }

} catch (InterruptedException ex) { ... handle ...}

}
Object produce() { ... }
}

class Consumer implements Runnable {

private final BlockingQueue queue;

Consumer(BlockingQueue q) { queue = q; }

public void run() {

try {

while(true) { consume(queue.take()); }

} catch (InterruptedException ex) { ... handle ...}

}

void consume(Object x) { ... }

}

class Setup {

void main() {

BlockingQueue q = new SomeQueueImplementation();

Producer p = new Producer(q);

Consumer c1 = new Consumer(q);

Consumer c2 = new Consumer(q);

new Thread(p).start();

new Thread(c1).start();

new Thread(c2).start();
}

}


   1. import java.util.concurrent.locks.Condition;  
2. import java.util.concurrent.locks.Lock;
3. import java.util.concurrent.locks.ReentrantLock;
4.
5. public class BoundedBuffer<T> {
6. //
7. private int head;
8. private int tail;
9. private int count;
10. private final T buffer[];
11.
12. //
13. private final Lock lock = new ReentrantLock();
14. private final Condition notEmpty = lock.newCondition();
15. private final Condition notFull = lock.newCondition();
16.
17. @SuppressWarnings("unchecked")
18. public BoundedBuffer(int capacity) {
19. this.buffer = (T[]) new Object[capacity];
20. }
21.
22. public T take() throws InterruptedException {
23. lock.lock();
24. try {
25. while(isEmpty()) {
26. notEmpty.await();
27. }
28.
29. T t = doTake();
30.
31. notFull.signal();
32.
33. return t;
34. } finally {
35. lock.unlock();
36. }
37. }
38.
39. public void put(T t) throws InterruptedException {
40. lock.lock();
41. try {
42. while(isFull()) {
43. notFull.await();
44. }
45.
46. doPut(t);
47.
48. notEmpty.signal();
49. } finally {
50. lock.unlock();
51. }
52. }
53.
54. private boolean isEmpty() {
55. return count == 0;
56. }
57.
58. private boolean isFull() {
59. return count == buffer.length;
60. }
61.
62. private T doTake() {
63. T t = buffer[head];
64. buffer[head] = null;
65. if(++head == buffer.length) {
66. head = 0;
67. }
68. --count;
69. return t;
70. }
71.
72. private void doPut(T t) {
73. buffer[tail] = t;
74. if(++tail == buffer.length) {
75. tail = 0;
76. }
77. ++count;
78. }
79. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值