Java Concurrency Programming Small memory

Source: Internet
Author: User

1. Semaphore

A semaphore is a counter that is used to protect access to one or more shared resources. If a thread wants to access a shared resource, it must first obtain a semaphore. If the internal counter is greater than 0, minus 1, or equal to 0, the thread goes to sleep until the counter is greater than or equal to 0.

 Semaphore semaphore1 = 1 ); //  Semaphore semaphore2 = new  Semaphore (1 , Span style= "color: #0000ff;" >true ); //  Fair mode, FIFO  semaphore1.acquire (); //  Gets the resource, otherwise blocked, and can be interrupted during the blocking process  semaphore1.acquireuninterruptibly (); //  Gets the resource, otherwise blocked, and cannot be interrupted during blocking  Semaphore1.tryacquire (); //  try to get  semaphore1.release (); //  release  
2. Countdownlatch

Countdownlatch is a synchronous helper class that allows a thread to wait until it completes a set of operations that are being performed in another thread. If a thread needs to wait for some operation to finish first, call the await () method to Hibernate, and when an operation completes, call Countdown () to subtract 1 from the internal counter and, if it becomes 0 o'clock, wake up all threads that have entered hibernation because of the call to await ().

The countdownlatch mechanism is not used to protect shared resources or critical sections, it is one or more threads used to synchronize multiple tasks.

Countdownlatch cannot be reset.

New // counter is number // counter minus 1 // gets the value of the counter // wait for counter to change 0
3. Cyclicbarrier

Cyclicbarrier is a synchronous helper class that allows two or more threads to synchronize at a point. When a thread reaches the specified point, calls the await () method to hibernate and waits for the other thread, and when the last await () method is called, Cyclicbarrier wakes all the waiting threads and then those threads will continue to execute.

Cyclicbarrier can also pass in another Runnable object as a thread that runs later.

Cyclicbarrier can be reset.

Cyclicbarrier barrier =NewCyclicbarrier (5, Task);//requires a collection of 5 threads and then runs the taskBarrier.await ();//Hibernate waitBarrier.await (Long, Timeunit);//stay dormant until it is interrupted. or the internal counter becomes 0, or the time expiresBarrier.getnumberwaiting ();//returns the number of blocked threadsBarrier.getparties ();//returns the number of tasks synchronized by the objectBarrier.reset ();//Resets the object, and the await () thread throws a brokenbarrierexceptionBarrier.isbroken ();//determines whether the object is corrupt, which occurs when one of the threads is interrupted and thrown interruptedexception
4. Phaser

An auxiliary class that allows concurrent multi-stage tasks to be synchronized at the end of each step.

You need to initialize the number of tasks for the synchronization operation, but you can dynamically increase or decrease the number of tasks.

The phaser is not interrupted by a response.

New Phaser (3//  requires 3 threads to synchronize //  wait 3 threads to reach this point of execution //  reduce a thread that needs to be synchronized // returns true to indicate that the phaser.isfinalized () is in the terminating state .    

Phaser also provides other ways to change objects.

Phaser Phaser =NewPhaser (3);//requires 3 thread synchronizationPhaser.arrive ();//notifies a participant that the current stage has been completed and should not wait for other participants to synchronizePhaser.awaitadvance (int);//If the incoming parameter is consistent with the current stage, the current thread sleeps until this stage the participant completes;Phaser.awaitadvanceinterruptibly (int);//throws an exception if interruptedPhaser.register ();//sign up for a new participantPhaser.register (int);//Bulk RegistrationPhaser.forcetermination ();//forced termination 
5. Exchanger

An auxiliary class that allows you to define a synchronization point between two threads, exchanging their data structure when two threads reach the synchronization point.

New Exchanger<list<string>>(); ListNew list<string>= exchanger.exchange (buffer);

Java Concurrency Programming Small memory

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.