Java 5 multi-thread (5)-cyclicbarrier synchronization tool class

Source: Internet
Author: User

Allows a series of sets to wait for each other to reach a common obstacle point. it means that everyone is waiting for each other. After the gathering, everyone starts to set up. After the scattered activities, they will meet at the designated place. This is like the whole company's staff taking advantage of the weekend outing, start from the house to the company, start to the park at the same time, and then start dining at the designated place ,... public static void main (string [] ARGs ){

Executorservice service = executors. newcachedthreadpool (); Final javasicbarrier cb = new javasicbarrier (3); For (INT I = 0; I <3; I ++) {runnable = new runnable () {public void run () {try {thread. sleep (long) (math. random () * 10000); system. out. println ("Thread" + thread. currentthread (). getname () + "coming to collection location 1, current" + (CB. getnumberwaiting () + 1) + "arrived," + (CB. getnumberwaiting () = 2? "It's all done, continue": "Waiting"); CB. await (); // only when all three threads have arrived can we go down to thread. sleep (long) (math. random () * 10000); system. out. println ("Thread" + thread. currentthread (). getname () + "coming to collection Location 2, current" + (CB. getnumberwaiting () + 1) + "arrived," + (CB. getnumberwaiting () = 2? "It's all done, continue": "Waiting"); CB. await (); thread. sleep (long) (math. random () * 10000); system. out. println ("Thread" + thread. currentthread (). getname () + "coming to collection location 3, current" + (CB. getnumberwaiting () + 1) + "arrived," + (CB. getnumberwaiting () = 2? "It's all done, continue": "Waiting"); CB. await ();} catch (exception e) {e. printstacktrace () ;}}; service.exe cute (runnable);} service. shutdown ();

}


There is an example of using javasicbarrier In the API:Class solver {
Final int N;
Final float [] [] data;
Final javasicbarrier barrier;

Class worker implements runnable {
Int myrow;
Worker (INT row) {myrow = row ;}
Public void run (){
While (! Done ()){
Processrow (myrow );

Try {
Barrier. await (); // after processing a row, wait here. If n threads have completed their respective rows, they will wait here, and then execute new javasicbarrier (n, runnbale) runnable code, that is, merge mergerows ();
} Catch (interruptedexception ex ){
Return;
} Catch (brokenbarrierexception ex ){
Return;
}
}
}
}

Public solver (float [] [] matrix ){
Data = matrix;
N = matrix. length;
Barrier = new cyclicbarrier (n,
New runnable (){
Public void run (){
Mergerows (...);
}
});
For (INT I = 0; I <n; ++ I)
New thread (new worker (I). Start ();

Waituntildone ();
}
}
Here, each worker thread processes this matrix row and waits at this obstacle to know that all rows of the matrix are processed, when all rows are processed and the supplied runnable obstacle is executed and merged, if the merge operator determines a merge scheme, then return (return), and each worker will terminate. reprinted please indicate the source: http://blog.csdn.net/johnny901114/article/details/8695723

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.