package com.bjsxt.base.coll013; import java.util.concurrent.SynchronousQueue; /** ConcurrentLinkedQueue : FIFO high concurrent queue. No locks, no blocking. Null elements are not allowed. Advanced at the head, backward at the end. Methods: add( ), offer(), Poll() take elements from the head, and delete, peek() removes the element from the head, not delete. BlockingQueue interface: blocking queue ArrayBlockingQueue: Does not support concurrency, inefficient. There is a length limit, and blocking, no separation of read and write (meaning that production and consumption cannot be performed at the same time) LinkedBlockingQueue: Supports concurrent operations and is efficient. Blocking queue based on linked list, efficient processing of concurrent data, no length limit, internal implementation of read-write separation lock, This enables the producer and consumer operations to run completely in parallel. SynchronousQueue: An unbuffered queue that generates one and consumes one, and there is no second. * */ public class UseQueue { public static void main(String[] args) throws Exception { //High-performance non-blocking unbounded queue: ConcurrentLinkedQueue //First in first out high concurrent queue. No locks, no blocking. Null elements are not allowed. /* ConcurrentLinkedQueue<String> q = new ConcurrentLinkedQueue<String>(); q.offer("a"); q.offer("b"); q.offer("c"); q.offer("d"); q.add("e"); System.out.println(q.poll()); //a takes the element from the head and deletes it from the queue System.out.println(q.size()); //4 System.out.println(q.peek()); //b is not removed, the next peek is still this b System.out.println(q.size()); //4 */ /*ArrayBlockingQueue<String> array = new ArrayBlockingQueue<String>(5); array.put("a"); array.put("b"); array.add("c"); array.add("d"); array.add("e"); array.add("f"); System.out.println(array.offer("a", 3, TimeUnit.SECONDS));*///Wait for 3 seconds, throw an exception if the addition is unsuccessful. // blocking queue //Support concurrent operations, efficient. Blocking queue based on linked list, efficient processing of concurrent data, no length limit, internal implementation of read-write separation lock, //Therefore, the producer and consumer operations can run completely in parallel. /* LinkedBlockingQueue<String> q = new LinkedBlockingQueue<String>(); q.offer("a"); q.offer("b"); q.offer("c"); q.offer("d"); q.offer("e"); q.add("f"); //System.out.println(q.size()); for (Iterator iterator = q.iterator(); iterator.hasNext();) { String string = (String) iterator.next(); System.out.println(string); } System.out.println("---------------------------------"); List<String> list = new ArrayList<String>(); System.out.println(q.drainTo(list, 3)); System.out.println(list.size()); for (String string : list) { System.out.println(string); }*/ final SynchronousQueue<String> q = new SynchronousQueue<String>(); Thread t1 = new Thread(new Runnable() { @Override public void run() { try { System.out.println(q.take()); } catch (InterruptedException e) { e.printStackTrace (); } } }); t1.start(); Thread.sleep(100); Thread t2 = new Thread(new Runnable() { @Override public void run() { try { q.put("ddd"); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace (); } } }); t2.start(); } }