await/signal/lock实现生产消费者模式
2015-08-12 11:41
501 查看
存储类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);
}
}
生产者:
[java]
view plaincopyprint?
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();
}
}
}
}
消费者:
[java]
view plaincopyprint?
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();
}
}
}
}
运行主类:
[java]
view plaincopyprint?
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
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);
}
}
生产者:
[java]
view plaincopyprint?
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();
}
}
}
}
消费者:
[java]
view plaincopyprint?
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();
}
}
}
}
运行主类:
[java]
view plaincopyprint?
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
相关文章推荐
- wait/notify实现生产消费者模式
- CoreData之条件查询 contains
- LeetCode题解:Contains Duplicate II
- CoreData之条件查询 contains
- LeetCode题解:Contains Duplicate
- Dynamics CRM 开启EmailRouter日志记录
- Dynamics CRM 开启EmailRouter日志记录
- Dynamics CRM 开启EmailRouter日志记录
- Dynamics CRM 开启EmailRouter日志记录
- 再谈应用环境下的TIME_WAIT和CLOSE_WAIT
- 新型程序设计语言:Braise语言!
- HDU 3394 双连通分量 桥 Railway
- Contains Duplicate III
- 解决Failed to allocate memory: 8
- 2015 Multi-University Training Contest 7 hdu 5371 Hotaru's problem
- 如何获取经纬度之间两点间真实距离(适用于GoogleMap,BaiduMap,Amap等)
- 报错:failed to get the task for process XXX(解决方案)
- 一个epoll/aio/eventfd结合使用的简单例子
- 数据挖掘十大算法——Naive Bayes
- Failed to load class "org.slf4j.impl.StaticLoggerBinder问题故障的解决