Blockingqueue principle Analysis

Source: Internet
Author: User

Reentrantlock two two relatively large features.
1. Interrupts
2. Timing
3. Fair lock.

Readwritelock

Read and read not mutually exclusive
Read-Write Mutex
Write the mutex.
Condition similar to Object.wait () and object.notify () and synchronized companion use

Countdownlatch Lanchi
Synchronized

Static final Countdownlatch end = new Countdownlatch (10);
End.countdown ();
End.await ();

The Cyclicbarrier loop fence Countdownlatch can be reused.

Semaphore
Shared locks
Run multiple threads at the same time critical section

Arrayblockingqueue

Reentrantlock
+ Two X condition

/** Main Lock Guarding All access */
Final Reentrantlock lock;
/** Condition for waiting takes */
Private final Condition Notempty;
/** Condition for waiting puts */
Private final Condition Notfull;


Offer
Take
When writing. Judging condition of lock unlock
If it is full, if it is slow, wait for the notification condition of condition.

Call Insert Insert and then Notempty


Blockingqueue


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 ();
}
}


private void Insert (E x) {
Items[putindex] = x;
Putindex = Inc (PUTINDEX);
++count;
Notempty.signal ();
}

Blockingqueue principle Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.