Advanced synchronization of jdk5.0 in Java (j2se entry 27)

Source: Internet
Author: User

Jdk5.0 advanced Synchronization

Semaphore class (semaphore)
That is, you can allocate a license to the thread and specify the number of licenses to synchronize multiple threads.

Semaphore S = new semaphore (4); // four licenses can be allocated. When all licenses are allocated, threads that do not get a license will be blocked.

Acquire () method to obtain a license. Release a license using the release () method. There are also methods for specifying the number of releases and licenses.

Countdownlatch class

Countdownlatch has a counter. The objects accessing this class will be reduced by one from the counter. The countdown () method will subtract one from the originally set counter value, when the countdown counter is zero, all await () threads will be put.

Cyclicbarrier class

Cyclicbarrier and countdownlatch are similar.
Cyclicbarrier provides the number of threads during construction. Only the number of waiting threads reaches the number specified in the constructor. When the last thread waits, all threads will be released, this class is a multi-thread convergence tool.

Exchanger class
The Exchange () method can be used to exchange objects between two threads, waiting for the second thread at the synchronization point of the two threads. During synchronization, objects are exchanged and released at the same time.

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.