存储类GoodStorage:
package main.wll.fish.product;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class GoodStorage {
private static final int SIZE = 2;
private List<Good> storage = new ArrayList<Good>(SIZE);
// 锁
private final Lock lock = new ReentrantLock();
// 仓库满的条件变量
private final Condition full = lock.newCondition();
// 仓库空的条件变量
private final Condition empty = lock.newCondition();
public void add(Good good) {
lock.lock();
{
while (storage.size() >= SIZE) {
try {
System.out.println(Thread.currentThread().getName() + "------>product wait.... now ListSIze:"
+ storage.size());
full.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
storage.add(good);
System.out.println(Thread.currentThread().getName() + "------>product :" + good.toString()
+ ", now ListSIze:" + storage.size());
empty.signalAll();
}
lock.unlock();
}
public void remove() {
lock.lock();
{
while (storage.size() == 0) {
try {
System.out.println(Thread.currentThread().getName() + "------>consume wait.... now ListSIze:"
+ storage.size());
empty.await();
} 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());
full.signalAll();
}
lock.unlock();
}
}
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-8------>product wait.... now ListSIze:2
pool-1-thread-12------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-7------>product wait.... now ListSIze:2
pool-1-thread-13------>product wait.... now ListSIze:2
pool-1-thread-9------>product wait.... now ListSIze:2
pool-1-thread-17------>product wait.... now ListSIze:2
pool-1-thread-16------>product wait.... now ListSIze:2
pool-1-thread-5------>product wait.... now ListSIze:2
pool-1-thread-20------------------------------------>consume :Good-34532, now ListSIze:1
pool-1-thread-20------------------------------------>consume :Good-34533, now ListSIze:0
pool-1-thread-20------>consume wait.... now ListSIze:0
pool-1-thread-21------>consume wait.... now ListSIze:0
pool-1-thread-6------>product :Good-34534, now ListSIze:1
pool-1-thread-6------>product :Good-34535, now ListSIze:2
pool-1-thread-6------>product wait.... now ListSIze:2
pool-1-thread-10------>product wait.... now ListSIze:2
pool-1-thread-2------>product wait.... now ListSIze:2
pool-1-thread-14------>product wait.... now ListSIze:2
pool-1-thread-18------>product wait.... now ListSIze:2
pool-1-thread-3------>product wait.... now ListSIze:2
pool-1-thread-11------>product wait.... now ListSIze:2
pool-1-thread-15------>product wait.... now ListSIze:2
pool-1-thread-19------>product wait.... now ListSIze:2
pool-1-thread-4------>product wait.... now ListSIze:2
pool-1-thread-8------>product wait.... now ListSIze:2
pool-1-thread-12------>product wait.... now ListSIze:2
pool-1-thread-1------>product wait.... now ListSIze:2
pool-1-thread-7------>product wait.... now ListSIze:2
pool-1-thread-13------>product wait.... now ListSIze:2
pool-1-thread-9------>product wait.... now ListSIze:2
pool-1-thread-17------>product wait.... now ListSIze:2
pool-1-thread-16------>product wait.... now ListSIze:2
pool-1-thread-5------>product wait.... now ListSIze:2
pool-1-thread-20------------------------------------>consume :Good-34534, now ListSIze:1
pool-1-thread-20------------------------------------>consume :Good-34535, now ListSIze:0
pool-1-thread-20------>consume wait.... now ListSIze:0
pool-1-thread-21------>consume wait.... now ListSIze:0