您的位置:首页 > 大数据 > 人工智能

wait/notify实现生产消费者模式

2015-08-12 11:27 579 查看
存储类GoodStorage:import java.util.ArrayList;
import java.util.List;

public class GoodStorage {
private static final int SIZE = 2;
private List<Good> storage = new ArrayList<Good>(SIZE);

public void add(Good good){
synchronized (storage){
while (storage.size() >= SIZE){
try {
System.out.println(Thread.currentThread().getName() + "------>product wait.... now ListSIze:" +storage.size());
storage.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

storage.add(good);
System.out.println(Thread.currentThread().getName() + "------>product :"+ good.toString() + ", now ListSIze:" +storage.size());

storage.notifyAll();
}
}

public void remove(){
synchronized (storage){
while (storage.size() == 0){
try {
System.out.println(Thread.currentThread().getName() + "------>consume wait.... now ListSIze:" +storage.size());
storage.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

Good good = storage.get(0);
storage.remove(0);
System.out.println(Thread.currentThread().getName() + "------------------------------------>consume :"+ good.toString()+ ", now ListSIze:" +storage.size());

storage.notifyAll();
}
}

}

class Good {
public long id;

public Good(long id) {
this.id = id;
}

public String toString() {
return String.format("Good-%d", this.id);
}
}

生产者:

import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

class Producter implements Runnable {
private static AtomicInteger ato = new AtomicInteger(0);

private GoodStorage store;
public Producter(GoodStorage store ){
this.store = store;
}

public void run() {
while(true){
try {
Thread.currentThread().sleep(new Random().nextInt(1000));
// queue.put(new Good(System.currentTimeMillis()));
store.add(new Good(ato.incrementAndGet()));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


消费者:
import java.util.Random;

class Consumer implements Runnable {
private GoodStorage store;
public Consumer(GoodStorage store ){
this.store = store;
}

public void run() {
while(true){
try {
Thread.currentThread().sleep(new Random().nextInt(1000));
store.remove();

} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

运行主类:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Main {
static ExecutorService pool = Executors.newCachedThreadPool();

public static void main(String[] args) {
GoodStorage store = new GoodStorage();
pool.execute(new Producter(store));
pool.execute(new Producter(store));
pool.execute(new Producter(store));

pool.execute(new Consumer(store));
pool.execute(new Consumer(store));
}
}


运行结果:
pool-1-thread-4------>consume wait.... now ListSIze:0
pool-1-thread-1------>product :Good-1, now ListSIze:1
pool-1-thread-4------------------------------------>consume :Good-1, now ListSIze:0
pool-1-thread-3------>product :Good-2, now ListSIze:1
pool-1-thread-1------>product :Good-3, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-2, now ListSIze:1
pool-1-thread-1------>product :Good-5, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-3, now ListSIze:1
pool-1-thread-1------>product :Good-7, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-5, now ListSIze:1
pool-1-thread-3------>product :Good-4, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-7, now ListSIze:1
pool-1-thread-1------>product :Good-9, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-4, now ListSIze:1
pool-1-thread-2------>product :Good-6, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-9, now ListSIze:1
pool-1-thread-3------>product :Good-8, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-6, now ListSIze:1
pool-1-thread-3------>product :Good-10, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-8, now ListSIze:1
pool-1-thread-2------>product :Good-11, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-10, now ListSIze:1
pool-1-thread-2------>product :Good-14, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-11, now ListSIze:1
pool-1-thread-1------>product :Good-12, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-14, now ListSIze:1
pool-1-thread-3------>product :Good-13, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-12, now ListSIze:1
pool-1-thread-2------>product :Good-15, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-13, now ListSIze:1
pool-1-thread-5------------------------------------>consume :Good-15, now ListSIze:0
pool-1-thread-3------>product :Good-16, now ListSIze:1
pool-1-thread-1------>product :Good-17, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-16, now ListSIze:1
pool-1-thread-2------>product :Good-18, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-17, now ListSIze:1
pool-1-thread-5------------------------------------>consume :Good-18, now ListSIze:0
pool-1-thread-3------>product :Good-19, now ListSIze:1
pool-1-thread-2------>product :Good-20, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-19, now ListSIze:1
pool-1-thread-3------>product :Good-21, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-20, now ListSIze:1
pool-1-thread-1------>product :Good-22, now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-21, now ListSIze:1
pool-1-thread-4------------------------------------>consume :Good-22, now ListSIze:0
pool-1-thread-2------>product :Good-23, now ListSIze:1
pool-1-thread-3------>product :Good-24, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-23, now ListSIze:1
pool-1-thread-3------>product :Good-27, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-24, now ListSIze:1
pool-1-thread-3------>product :Good-28, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-27, now ListSIze:1
pool-1-thread-3------>product :Good-29, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-28, now ListSIze:1
pool-1-thread-1------>product :Good-25, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-29, now ListSIze:1
pool-1-thread-2------>product :Good-26, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-25, now ListSIze:1
pool-1-thread-1------>product :Good-31, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-26, now ListSIze:1
pool-1-thread-2------>product :Good-32, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-31, now ListSIze:1
pool-1-thread-1------>product :Good-33, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-32, now ListSIze:1
pool-1-thread-3------>product :Good-30, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-33, now ListSIze:1
pool-1-thread-3------>product :Good-35, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-30, now ListSIze:1
pool-1-thread-1------>product :Good-36, now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-35, now ListSIze:1
pool-1-thread-2------>product :Good-34, now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-36, now ListSIze:1
pool-1-thread-1------>product :Good-37, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-34, now ListSIze:1
pool-1-thread-2------>product :Good-40, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-37, now ListSIze:1
pool-1-thread-3------>product :Good-38, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-4------------------------------------>consume :Good-40, now ListSIze:1
pool-1-thread-2------>product :Good-41, now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-5------------------------------------>consume :Good-38, now ListSIze:1
pool-1-thread-1------>product :Good-39, now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  waitnotify