美文网首页
阻塞队列——BlockingQueue

阻塞队列——BlockingQueue

作者: Maxi_Mao | 来源:发表于2017-11-16 14:31 被阅读0次

阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞。试图从空的阻塞队列中获取元素的线程将会被阻塞,直到其他的线程往空的队列插入新的元素。同样,试图往已满的阻塞队列中添加新元素的线程同样也会被阻塞,直到其他的线程使队列重新变得空闲起来,如从队列中移除一个或者多个元素,或者完全清空队列。#### wait、notifyAll实现方式```package blocks;import java.util.ArrayList;/** * Created by maxi on 2017/11/8. */public class ObjectBlockQueuesimplements BlockQueues{ private final ArrayListlists; private int limite;// private ReentrantLock mLocks; public ObjectBlockQueues() { this(10, false); } public ObjectBlockQueues(int limite) { this(limite, false); } public ObjectBlockQueues(int limite, Boolean isFair) { this.limite = limite; lists = new ArrayList(limite);// mLocks = new ReentrantLock(isFair); } @Override public synchronized E get(int index) {// final ReentrantLock mLocks = this.mLocks;// mLocks.lock(); E e = this.lists.get(index);// mLocks.unlock(); return e; } @Override public void add(E e) { checkNotNull(e);// final ReentrantLock mLocks = this.mLocks;// mLocks.lock(); synchronized(this.lists) { try { while (this.lists.size() == this.limite) {// System.out.println("============" + e + " will wait ============="); this.lists.wait();// System.out.println("============" + e + " is break! ============="); } if (this.lists.size() == 0) {// System.out.println("============add " + e + " notifyAll ============="); this.lists.notifyAll(); } this.lists.add(e); } catch (Exception e1) { e1.printStackTrace(); } finally {// mLocks.unlock(); } } } @Override public void remove(E e) { checkNotNull(e);// final ReentrantLock mLocks = this.mLocks;// mLocks.lock(); synchronized(this.lists) { try { while (this.lists.size() == 0) { this.lists.wait(); } if (this.lists.size() == this.limite) {// System.out.println("============remove " + e + " notifyAll ============="); this.lists.notifyAll(); } this.lists.remove(e); } catch (Exception e1) { e1.printStackTrace(); } finally {// mLocks.unlock(); } } } @Override public synchronized void removeAll() {// final ReentrantLock mLocks = this.mLocks;// mLocks.lock(); this.lists.clear();// mLocks.unlock(); } @Override public int size() {// final ReentrantLock mLocks = this.mLocks;// mLocks.lock(); synchronized(this.lists) { int length = this.lists.size();// mLocks.unlock(); return length; } } private static void checkNotNull(Object v) { if (v == null) throw new NullPointerException(); }}```#### 并发类 锁的实现方式```package blocks;import java.util.ArrayList;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * Created by maxi on 2017/11/8. */public class ArrayBlockQueueimplements BlockQueues{ private final ArrayListlists; private int limite; private ReentrantLock mLocks; private Condition notEmpty; private Condition notFull; public ArrayBlockQueue() { this(10, false); } public ArrayBlockQueue(int limite) { this(limite, false); } public ArrayBlockQueue(int limite, Boolean isFair) { this.limite = limite; lists = new ArrayList<>(); mLocks = new ReentrantLock(isFair); notEmpty = mLocks.newCondition(); notFull = mLocks.newCondition(); } @Override public E get(int index) { final ReentrantLock mLocks = this.mLocks; mLocks.lock(); E e = this.lists.get(index); mLocks.unlock(); return e; } @Override public void add(E e) { checkNotNull(e);// final ReentrantLock mLocks = this.mLocks; mLocks.lock(); try { while (this.lists.size() == this.limite) {// System.out.println("============" + e + " will wait ============="); notFull.await();// System.out.println("============" + e + " is break! ============="); }// if (this.lists.size() == 0) {//// System.out.println("============add " + e + " notifyAll =============");// this.lists.notifyAll();// } this.lists.add(e); notEmpty.signalAll(); } catch (Exception e1) { e1.printStackTrace(); } finally { mLocks.unlock(); } } @Override public void remove(E e) { checkNotNull(e);// final ReentrantLock mLocks = this.mLocks; mLocks.lock(); try { while (this.lists.size() == 0) {// this.lists.wait(); notEmpty.await(); }// if (this.lists.size() == this.limite) {//// System.out.println("============remove " + e + " notifyAll =============");// this.lists.notifyAll();// } this.lists.remove(e); notFull.signalAll(); } catch (Exception e1) { e1.printStackTrace(); } finally { mLocks.unlock(); } } @Override public synchronized void removeAll() {// final ReentrantLock mLocks = this.mLocks; mLocks.lock(); this.lists.clear(); mLocks.unlock(); } @Override public int size() {// final ReentrantLock mLocks = this.mLocks; mLocks.lock(); int length = this.lists.size(); mLocks.unlock(); return length; } private static void checkNotNull(Object v) { if (v == null) throw new NullPointerException(); }}```#### main函数```package blocks;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.BlockingQueue;/** * Created by maxi on 2017/11/8. */public class BlockMain { public static void main(String[] args) throws InterruptedException {// BlockQueuesblockQueues = new ObjectBlockQueues<>(); BlockingQueueblockingQueues = new ArrayBlockingQueue(10); BlockQueuesblockQueues = new ArrayBlockQueue(10);

Thread thread1 = new Thread(() -> {

for (int i = 0; i <= 20; i++) {

//                try {

//                    Thread.sleep(1000);

//                } catch (InterruptedException e) {

//                    e.printStackTrace();

//                }

blockQueues.add("num" + i);

System.out.println("thread 1 add " + i + " queue size = " + blockQueues.size());

}

});

Thread thread2 = new Thread(() -> {

for (int i = 0; i <= 20; i++) {

try {

Thread.sleep(1000);

} catch (InterruptedException e) {

e.printStackTrace();

}

blockQueues.remove("num" + i);

System.out.println("thread 2 remove " + i + " queue size = " + blockQueues.size());

}

});

thread1.start();

thread2.start();

}

}

```

相关文章

  • 26. 并发终结之BlockingQueue

    线程池里面最重要的还有个并发容器,即阻塞队列BlockingQueue。BlockingQueue是阻塞队列的接口...

  • 阻塞队列 BlockingQueue

    阻塞队列 BlockingQueue BlockingQueue用法 BlockingQueue 通常用于一个线...

  • Java并发编程:阻塞队列

    Java并发编程:阻塞队列BlockingQueue 以上7类阻塞队列中有LinkedBlockingQueue,...

  • 探讨阻塞队列和线程池源码

    阻塞队列 非阻塞队列是一个先进先出的单向队列(Queue),而BlockingQueue阻塞队列实际是非阻塞队列的...

  • Android中的线程与线程池

    阻塞队列BlockingQueue 阻塞队列常用于生产者——消费者模型,生产者往阻塞队列插入数据,消费者往阻塞队列...

  • 阻塞队列

    BlockingQueue线程池的数据结构是阻塞队列BlockingQueue。(在多线程领域:所谓阻塞,在某些情...

  • Java并发-22.阻塞队列

    阻塞队列(BlockingQueue)是一种支持两个附加操作的队列: 支持阻塞的插入:队列满时,队列阻塞插入元素的...

  • 阻塞队列

    阻塞队列BlockingQueue不支持插入null元素,好好了解一下阻塞队列接口设计。 了解阻塞队列的接口方法后...

  • Java并发包之BlockingQueue

    一、什么是BlockingQueue BlockingQueue即阻塞队列,从阻塞这个词可以看出,在某些情况下对阻...

  • 19-阻塞队列之ArrayBlockingQueue

    Java中的阻塞队列 什么是阻塞队列 阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这两个附...

网友评论

      本文标题:阻塞队列——BlockingQueue

      本文链接:https://www.haomeiwen.com/subject/fmlztxtx.html