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