并发库学习笔记二

阻塞队列(生产者和消费者开发模式)

三种常用的阻塞队

ArrayBlockingQueue<T>;

LinkedBlockingQueue<T>;

SynchronousQueue<T>

 

//在队列满时被阻塞

ArrayBlockingQueue<String>arrayBlockingQueue=newArrayBlockingQueue<String>(19);

try{

arrayBlockingQueue.put("");

}catch(InterruptedExceptione){

e.printStackTrace();

}

 

 

 

//如果队列为空则阻塞

ArrayBlockingQueue<String>arrayBlockingQueue=newArrayBlockingQueue<String>(19);

try{

arrayBlockingQueue.take();

}catch(InterruptedExceptione){

e.printStackTrace();

}

 

使用阻塞队列

 

executorService=Executors.newSingleThreadExecutor();

 

 

TaskWithResulttaskWithResult=newTaskWithResult();

Future<String>future=executorService.submit(taskWithResult);

try{

future.get();

 

}catch(InterruptedExceptione){

e.printStackTrace();

}catch(ExecutionExceptione){

e.printStackTrace();

}

finalArrayBlockingQueue<String>arrayBlockingQueue=newArrayBlockingQueue<String>(19);

try{

arrayBlockingQueue.take();

}catch(InterruptedExceptione){

e.printStackTrace();

}

 

classTaskWithResultimplementsCallable<String>{

 

@Override

publicStringcall()throwsException{

//不等待就直接返回

Stringresu=arrayBlockingQueue.poll();

//正确的方法应该是,等待到有数据才继续

Stringresu=arrayBlockingQueue.take();

//防止列等

Stringresu=arrayBlockingQueue.poll(1,TimeUtil.DAY);

 

returnresu;

}

 

}

 

 

//实现一个简单的阻塞队列

classBlockingQ{

privateObjectnoEmpty=newObject();

privateQueue<Object>linkedList=newLinkedList<Object>();

 

publicObjecttake()throwsInterruptedException{

synchronized(noEmpty){

if(linkedList.size()==0){

//要执行wait()操作,必须先得取得对象的锁

//执行wait()操作后,锁会释放

noEmpty.wait();

}

returnlinkedList.poll();

}

}

 

publicvoidoffer(Objectobject){

synchronized(noEmpty){

if(linkedList.size()==0){

//都必须取得对象的锁

noEmpty.notifyAll();

}

linkedList.add(object);

}

}

}

//实现一个简单的阻塞队列2

classBlockingQ{

privateObjectnoEmpty=newObject();

privateObjectnoFull=newObject();

privateQueue<Object>linkedList=newLinkedList<Object>();

privateintmaxLength=10;

 

publicObjecttake()throwsInterruptedException{

synchronized(noEmpty){

if(linkedList.size()==0){

noEmpty.wait();

}

synchronized(noFull){

//如果队列满

if(linkedList.size()==maxLength){

linkedList.notifyAll();

}

returnlinkedList.poll();

}

}

}

 

publicvoidoffer(Objectobject)throwsInterruptedException{

synchronized(noEmpty){

if(linkedList.size()==0){

//都必须取得对象的锁

noEmpty.notifyAll();

}

synchronized(noFull){

//如果队列满

if(linkedList.size()==maxLength){

noFull.wait();

}

}

linkedList.add(object);

}

}

}

 

 

//实现一个简单的阻塞队列3

classBlockingQ{

privateLocklock=newReentrantLock();

//一个锁可以创建多个condition

privateConditionnoEmpty=lock.newCondition();

privateConditionnoFull=lock.newCondition();

privateQueue<Object>linkedList=newLinkedList<Object>();

privateintmaxLength=10;

 

publicObjecttake()throwsInterruptedException{

lock.lock();

try{

if(linkedList.size()==0){

noEmpty.await();

}

//如果队列满

if(linkedList.size()==maxLength){

noFull.signalAll();

}

returnlinkedList.poll();

}finally{

lock.unlock();

}

}

 

publicvoidoffer(Objectobject)throwsInterruptedException{

lock.lock();

try{

if(linkedList.size()==0){

//要执行signal都必须取得对象的锁

noEmpty.signalAll();

}

//如果队列满

if(linkedList.size()==maxLength){

noFull.await();

}

linkedList.add(object);

}finally{

lock.unlock();

}

}

}

注:如果未锁就直接执行awaitsignalsiganlAll会抛异常

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值