Blockingqueue
A thread can know the existence of a b thread
An interface is not a specific implementation:
Arrayblockingqueue
The inner elements of the arrayblockingqueue are placed in an array of objects:final object[] items;
Offer (): Returns false immediately when the queue is full
Put (): Waits if the queue is full
Pool (): Popup element, null returned if empty
Take (): The popup element, if it is empty, wait until there are elements.
Take method:
Public e take () throws InterruptedException { final ReentrantLock lock = this.lock; lock.lockinterruptibly (); try { while (count == 0) notempty.await (); return extract (); } finally { lock.unlock (); } } private void insert (e x) { items[putindex] = x; putindex = inc (Putindex); ++count; notempty.signal (); }
Put method:
public void put (e e) throws Interruptedexception { checknotnull (e); final ReentrantLock lock = this.lock; lock.lockinterruptibly (); try { while (count == Items.length) Notfull.await (); insert (e); } finally { lock.unlock (); } }
/** * extracts element at current take position, advances, and signals. * Call only when holding lock. */ Private e extract () { final Object[] Items = this.items; e x = this.<e >cast (Items[takeindex]); items[takeindex] = null; takeindex = inc (Takeindex); --count; notfull.signal (); return x; }
linkedblockingqueue (Lock separation )
two different locks/** lock held by take, poll, etc */ private final ReentrantLock takeLock = new Reentrantlock (); /** wait queue for waiting takes * / private final condition notempty = takelock.newcondition (); /** Lock held by put, offer, etc */ private final reentrantlock putlock = new reentrantlock (); /** Wait queue for waiting puts */ private final condition notfull = putlock.newcondition ();
take function Public e take () throws InterruptedException { E x; int c = -1; final AtomicInteger count = this.count; final ReentrantLock takeLock = This.takelock; takelock.lockinterruptibly ();//cannot have two threads fetching data simultaneously try { while (Count.get () == 0) {//If there is no data, always wait (because it is lockinterruptibly, can be interrupted) notempty.await (); } x&nBsp;= dequeue ();//Get First Data c = count.getanddecrement ();//number-1, atomic operation, because the count will be accessed concurrently with the put. if (c > 1) notempty.signal ();// Notify other take operations } finally { takelock.unlock ();//Release lock } if (c == capacity) signalnotfull ();//notify put operation, free space available return x; }
put function Public void put (e e) throws InterruptedException { if (E == null) throw new nullpointerexception (); // note: convention in all put/take/ etc is to preset local var // holding count negative to indicate failure unless set. int c = -1; node <e> node = new node (E); final Reentrantlock putlock = this.putlock; final AtomicInteger count = this.count; Putlock.lockinterruptibly ();//lock cannot have twoThread to put function at the same time try { /* * note that count is used in wait guard even though it is * not protected by lock. this works because count can * only decrease at this point (all other puts are shut * out by lock), and we (or some other waiting put) are * Signalled if it ever changes from capacity. similarly * for all other uses of count in other wait guards. */ while (Count.get () == capacity) {//when the queue is full, wait notfull.await (); } enqueue (node);//Insert Data c = count.getandincrement ();//Update Total if (c + 1 < capacity) &nBsp; notfull.signal ();//have enough space to notify other threads } finally { putlock.unlock ();//Release lock } if (c == 0) signalnotempty ();//After a successful release, notify take function to fetch data }ArrayList under the concurrency
When the ArrayList is expanding, internal consistency is compromised, and because there is no lock protection, another thread cannot access the inconsistent internal state, resulting in a cross-border problem.
Multiple threads also appear to be assigned to the same location at the same time.
ConcurrentlinkedqueueConcurrentlinkedqueue:
A high concurrency environment can be said to be the best queue, or it can be seen as a thread-safe linkedlist.
copyonwritearraylist
Read-write list with good performance, no lock at any time when reading and writing, only need to wait synchronously while writing.
When writing, do a self-replication. Once the original data is copied and the modified content is written to the copy, the copy is replaced with the original data.
Priorityqueue
Priorityqueue is also called the priority queue, which refers to each element that is removed from the priority queue, either the maximum (maximum heap) or the minimum (minimum heap). We know that the queue is a first-in-one-out data structure, each time the team heads out (removing an element) and inserting an element (queued) from the end of the team.
Arraydeque (a double-ended queue)
Reference: http://blog.csdn.net/qq_35326718/article/details/72972159
A collection framework commonly used in Java