Java Concurrency-thread collaboration

Source: Internet
Author: User

This period of time a little busy, technical blog update less, today update the relevant concurrency of common thread collaboration class bar.

Executorservice

Thread pool, used to create and reuse threads, he has several modes.

Let me give you an example of a custom thread pool number:

Executorservice service = Executors.newfixedthreadpool (ten);

Countdownlatch

It is used primarily to inform the waiting thread when it can start working. Countdownlatch is generally used as an internal variable of a thread and passed through the constructor function

After a certain time in the thread is over, the call to the countdown method will automatically 1, and when the countdown counter is 0 o'clock, the waiting thread will continue to function.

Executorservice service = Executors.newfixedthreadpool (new countdownlatch (1 ) Service.submit (new  Threada (end)) end. await ();

Cyclicbarrier

Typically used to set a thread's collection point, using the same way as the Countdownlatch method, except that the await of this class requires the thread to wait for the internal counter to end completely until 0 before it continues to work.

When a thread Cheng less threads, it may be easy to deadlock. In addition, the secondary counter can be reused inside the thread

Cyclicbarrier barrier = new Cyclicbarrier (10);
Barrier.await ();

Semaphore

Typically used to set up a queueing mechanism where a method enters a maximum of x threads when a multi-threaded collaboration occurs. The usage is similar to the previous one, setting a maximum limit, but requires manual release after the operation is complete.

        New Semaphore (3);        Semaphore.acquire ();        Semaphore.release ();

Future

This method is used to asynchronously handle some complex operations, directly on the example bar by implementing the call method of the callable interface to wrap, asynchronously processing, and then through the future of the Get method callback.

 Public classTestforthread { Public intgetres () {Try{Thread.Sleep (Ten* +); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }        return  +; } @Test Public voidFultruetest () throws Interruptedexception, executionexception {executorservice service= Executors.newfixedthreadpool (Ten); Countdownlatch End=NewCountdownlatch (1); Semaphore Semaphore=NewSemaphore (3); Future<Integer> f = service.submit (NewCallable<integer>() {@Override PublicInteger Call () throws Exception {//TODO auto-generated Method Stub                return NewTestforthread (). Getres ();                }        }); System. out. println ("Keep on"); System. out. println ("Get Callable Data"); intA; Try{a= f.Get( the, Timeunit.seconds); System. out. println (a); } Catch(TimeoutException e) {//TODO auto-generated Catch blockSystem. out. println ("Timeout"); } System. out. println ("End"); End.await(); }}

Exchanger

This is used for switching between threads, such as a thread that reads a thread to write, and this class I thought about maybe to save a map space. You do not need to create a new map to hold temporary data.

Specific examples do not give, check the API is very simple. (Mostly I can't remember--)

Java Concurrency-thread collaboration

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.