Talk about concurrent Programming (vi): Introduction to some common concurrency components in Java

Source: Internet
Author: User

CountdownlatchIt is used to synchronize one or more tasks, forcing them to wait for a set of operations performed by other tasks to complete. You can set an initial count value to the Countdownlatch object, and any method that calls await () on this object will block until the count reaches 0. When other tasks end their work, you can call Countdown () on the object to reduce the count. The Countdownlatch is designed to be triggered only once, and the value cannot be reset.     If you need a version that resets the value, you can use Cylicbarrier.     The task calling countdown () is not blocked when the call is generated, and only the call to await () is blocked until the count value reaches 0. The typical usage of Countdownlatch is that the current program has n mutually independent, resolved tasks and creates a countdownlatch with a value of N. When each task is completed, Countdown () is called on the latch. The task waiting for the problem to be resolved invokes await () on the latch and stops itself until the latch count ends.
SummaryThe Countdownlatch simulates aMulti-task blocking wait-Multiple resolution TasksThis scenario of reaching a certain condition. If it is just a multi-tasking block waiting for a task, then using wait and signalall directly can be done simply. If you are waiting for multiple tasks to complete, using wait and Signalall must combine a count value with a lock to implement this scenario together. Java helps us abstract this scenario and the solution is named Countdownlatch.
CyclicbarrierCyclicbarrier applies to situations where you want to create a set of tasks that perform the work in parallel, and then wait until the next step, until all tasks are completed (it looks like join ()). It allows all parallel tasks to be queued at the fence so that they can move forward in a consistent way.
SummaryCountdownlatch like a referee's track and field competition, to wait until all the referees are in place, to open the run. Cyclicbarrier like a game without referees, only the players have to be in position to run straight away. Imagine how to implement this scenario with wait and signal, using a lock to control access to a count value, and if the count is greater than 1, wait on an object (which can be the Count value object), then-1, and if the count value is 1, signalall the object. Java helps us abstract this kind of scene, name Yue Cyclicbarrier.
DelayqueueThis is an unbounded blockingqueue for placing an object that implements the delayed interface, where an object can only be removed from the queue when it expires. This queue is ordered, that is, the delay of the team header object expires for the longest time.     If there is no delay due, then there will be no header element, and poll () will return null (because of this, you cannot put NULL in this queue). The delayed interface has a method named Getdelay (), which can be used to tell how long the delay expires, or how long the delay has expired before the method restricts us to using the Timeunit class.
SummaryFor a normal queue, it is a FIFO queue. The understanding of the Delayqueue queue can be thought of as having two queues, a priority queue for tasks that have not yet expired (from the team head to the end of the team, sorted from near to far from the expiry time), and then in turn the expiring task into an ordered queue, which is the queue we take when we really took it.     The component, such as its name, represents the time at which the object placed in the queue can control its own removal. In the usage scenario, the delayqueue applies to tasks that limit the amount of time before they are performed. The limit here is the time, not the other threads.
PriorityblockingqueueThe difference from ordinary blockingqueue is that the queue time is not rated in the queue time, but the priority is the highest level. A hero with time theory, a hero with a priority.
SynchronousqueueThis is a blocking queue with no internal capacity, so each put () must wait for a take () and vice versa. It's like you're handing over an object to someone--no table can hold the object, so you can only work when the person reaches out and is ready to receive the object.
ScheduledexecutorScheduledexecutor gives you a way to start threads on a regular basis. You can use Schedule () (Run a Task) or scheduleatfixedrate () (Repeat the task every regular time), and you can set the Runnable object to execute at some point in the future. Functionally similar to timers, which are used very frequently in web systems.
SemaphoreA normal lock (from a concurrent.locks or built-in synchronized lock) allows only one task to access a resource at any given time, while a count semaphore allows n tasks to access the resource at the same time. In fact, the semaphore is less used: 1. Resource access is mostly only 1 to 2 of the difference, there is no 2 to many differences, thread-safe object Multiple access is not a problem, thread unsafe object two thread access is problematic. 2. If the current usage scenario is that only n objects are serviced and can be implemented with semaphores, it may be better to actually use a blocking queue.
ExchangerExchanger is a fence that swaps objects between two tasks. When these tasks enter the fence, they each have an object, and when they leave, they all have objects previously held by the object. A typical scenario for exchanger is that a task is creating objects that are expensive to produce, while another task is consuming those objects. In this way, more objects can be consumed at the same time that they are created.


Talk about concurrent Programming (vi): Introduction to some common concurrency components in Java

Related Article

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.