Concurrency tool classes in Java

Source: Internet
Author: User

1, waiting for multi-threaded completion of the Countdownlatch

Countdownlatch allows one or more threads to wait for another thread to complete the operation. Join is used to let the current thread of execution wait for the join thread to finish. The principle is to keep checking to see if the join thread is alive and let the current thread wait forever if the join thread survives. The This.notifyall () method of the thread is called until the join thread aborts, and the call to Notifyall () method is implemented in the JVM.

2. Synchronous barrier Cyclicbarrier

Cylicbarrier literally means a barrier (Barrier) that can be recycled (Cyclic). It blocks a set of threads from reaching a barrier (also called a synchronization point) until the last thread reaches the barrier and the barrier opens, and all the threads that are blocked by the barrier continue to run.

Countdownlatch counters can only be used once, while cyclicbarrier counters may be reset using the rest () method. So cyclicbarrier can handle more complex business scenarios.

3, control the number of concurrent threads semaphore

Semaphore (semaphore) is the number of threads used to control access to a particular resource at the same time, by coordinating individual threads to ensure reasonable use of public resources.

Semaphore can be used for flow control , especially for applications with limited public resources, such as database connections.

Semaphore usage: The thread first uses the semaphore acquire () method to obtain a license, and then calls the release () method to return the license after use. You can also use the Tryacquire () method to try to obtain a license.

4. Exchanger of exchanging data between threads

Exchanger (the Exchange) is a tool class for collaboration between threads. Exchanger provides a synchronization point at which two threads can exchange data from each other. Call the Exchange () method separately.

Exchanger can be used for genetic algorithms, proofreading work.

Concurrency tool classes in Java

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.