JUC source code analysis - a collection of articles (nine) SynchronousQueue

JUC source code analysis - a collection of articles (nine) SynchronousQueue

JDK8 concurrent programming package SynchronousQueue is not a data buffer BlockingQueue, insert put its producer thread must wait for the consumer to remove the operation take, and vice versa.

SynchronousQueue can be seen as a passer, the producer is responsible for passing data directly to the consumer thread processing thread. Itself is not stored in any queue element, it is very suitable for transmission scenario. SynchronousQueue higher throughput and LinkedBlockingQueue ArrayBlockingQueue.

1. SynchronousQueue implementation principle

The principle SynchronousQueue Reference: http://ifeve.com/java-synchronousqueue/

1.1 blocking algorithm

Blocking algorithm commonly used in the interior of a lock to ensure that multiple threads put (), and take () method is performed serially. The use of lock overhead is relatively large, but also there is a case that needs thread A thread B holds lock, B must have been waiting for A to release the lock, even though A possible within a period of time because of the relatively high priority B and Debu time slice to run. So in high-performance applications, we often want to avoid the use of locks.

public class NativeSynchronousQueue<E> {
    boolean putting = false;
    E item = null;

    public synchronized E take() throws InterruptedException {
        while (item == null)
            wait();
        E e = item;
        item = null;
        notifyAll();
        return e;
    }

    public synchronized void put(E e) throws InterruptedException {
        if (e==null) return;
        while (putting)
            wait();
        putting = true;
        item = e;
        notifyAll();
        while (item!=null)
            wait();
        putting = false;
        notifyAll();
    }
}

1.2 Semaphore achieve

Classical synchronization queue implementation employs three semaphore, the code is very simple, relatively easy to understand:

public class SemaphoreSynchronousQueue<E> {
    E item = null;
    Semaphore sync = new Semaphore(0);
    Semaphore send = new Semaphore(1);
    Semaphore recv = new Semaphore(0);

    public E take() throws InterruptedException {
        recv.acquire();
        E x = item;
        sync.release();
        send.release();
        return x;
    }

    public void put (E x) throws InterruptedException{
        send.acquire();
        item = x;
        recv.release();
        sync.acquire();
    }
}

2. SynchronousQueue implementation principle

SynchronousQueue support fair access to the queue. By default thread non-equity strategy to access the queue. Use the following constructor to create fairness in access SynchronousQueue, if set to true, waiting threads will use the FIFO queue sequential access. For example to TransferQueue

TransferQueue data structure

reference:

  1. JUC source code analysis - a collection of articles (nine) SynchronousQueue
  2. JUC source code analysis - a collection of articles (nine) SynchronousQueue
  3. JUC source code analysis - a collection of articles (nine) SynchronousQueue

The intentions of recording a little bit every day. Perhaps the content is not important, but the habit is very important!

Guess you like

Origin www.cnblogs.com/binarylei/p/10927767.html