阻塞队列(生产者和消费者开发模式)
三种常用的阻塞队
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();
}
}
}
注:如果未锁就直接执行await、signal、siganlAll会抛异常