本文共 1807 字,大约阅读时间需要 6 分钟。
BlockingQueue是一个可以设置容量的阻塞队列的接口,如果没有进行容量限制则为Integer.MAX_VALUE的;
BlockingQueue实现主要用于生产者 - 消费者队列,但另外支持Collection接口。 已知的实现类有ArrayBlockingQueue DelayQueue LinkedBlockingDeque LinkedBlockingQueue LinkedTransferQueue PriorityBlockingQueue SynchronousQueue
BlockingQueue的方法有四种形式 * | 抛出异常 | 指定值 | 阻塞 | 超出时间 |
---|---|---|---|---|
插入 | add(e) | offer(e) | put(e) | offer(e, time, unit) |
移除 | remove() | poll() | take() | poll(time, unit) |
检查 | element() | peek() | 无 | 无 |
Exception in thread "main" java.lang.IllegalStateException: Queue full
;final BlockingQueuebasket = new LinkedBlockingQueue (2); basket.add("111"); basket.add("222"); try { new Thread() { @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } basket.poll(); System.out.println("MyClass.run"); } }.start(); boolean offer = basket.offer("333", 5000, TimeUnit.MILLISECONDS); System.out.println(offer); } catch (InterruptedException e) { e.printStackTrace(); }
转载地址:http://kxbsn.baihongyu.com/