注意:
1、在使用LinkedBlockingQueue时,若用默认大小且当生产速度大于消费速度时候,有可能会内存溢出。 2、在使用ArrayBlockingQueue和LinkedBlockingQueue分别对1000000个简单字符做入队操作时,LinkedBlockingQueue的消耗是ArrayBlockingQueue消耗的10倍左右, 即LinkedBlockingQueue消耗在1500毫秒左右,ArrayBlockingQueue只需150毫秒左右。 简单示例: BlockingQueue<String> queue = new LinkedBlockingQueue<>(2); boolean addOK = queue.add("a");// will throw IllegalStateException when Queue is full try { queue.put("b");// will blocking when Queue is full addOK = queue.offer("c");// will not blocking when Queue is full System.out.println(addOK); addOK = queue.offer("d", 1, TimeUnit.SECONDS);//will blocking the times when Queue is full System.out.println(addOK); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(queue); try { String value = queue.take(); // will blocking when Queue is empty value = queue.poll(); will not blocking when Queue is empty, return null if empty value = queue.poll(1, TimeUnit.SECONDS);//will blocking the times when Queue is empty, return null if empty queue.peek();//Retrieves, but does not remove, the head of this queue,null if this queue is empty } catch (InterruptedException e) { e.printStackTrace(); }