Java thread (26): new feature-Barrier

Source: Internet
Author: User

Java thread: The obstacle class is added to the new feature-obstacle Java 5. To meet a new design requirement, such as a large task, it is often necessary to assign many sub-tasks to execute. Only when all sub-tasks are completed can the sub-tasks be executed. At this time, you can select the barrier. Barrier is a method of multi-thread concurrency control, which is easy to use. The following is an example: import java. util. concurrent. brokenBarrierException; import java. util. concurrent. cyclicBarrier;/*** Java thread: new feature-barrier ** @ author leizhimin 10:50:10 */public class Test {public static void main (String [] args) {// create the barrier and set MainTask to the Runnable task when all the specified number of threads reach the barrier point. Required icbarrier cb = new CyclicBarrier (7, new MainTask (); new SubTask ("A", cb ). start (); new SubTask ("B", cb ). start (); New SubTask ("C", cb ). start (); new SubTask ("D", cb ). start (); new SubTask ("E", cb ). start (); new SubTask ("F", cb ). start (); new SubTask ("G", cb ). start () ;}/ *** */class MainTask implements Runnable {public void run () {System. out. println (">>>> The Chief Executive Officer is executed! <") ;}}/*** SubTask */class SubTask extends Thread {private String name; private javasicbarrier cb; SubTask (String name, CyclicBarrier cb) {this. name = name; this. cb = cb;} public void run () {System. out. println ("[subtask" + name + "] started to run! "); For (int I = 0; I <999999; I ++); // simulate time-consuming task System. out. println ("[subtask" + name + "] has been executed, and the barrier has been notified! "); Try {// The Notification barrier has completed cb. await ();} catch (InterruptedException e) {e. printStackTrace ();} catch (BrokenBarrierException e) {e. printStackTrace () ;}} running result: [subtask E] execution started! [Subtask E] The execution is completed, and the barrier has been notified! [Subtask F] execution started! [Subtask G] execution started! [Subtask F] The execution is completed, and the barrier has been notified! [Subtask G] The execution is completed, and the barrier has been notified! [Subtask C] execution started! [Subtask B] execution started! [Subtask C] The execution is completed, and the barrier has been notified! [Subtask D] execution started! [Subtask A] execution started! [Subtask D] The execution is completed, and the barrier has been notified! [Subtask B] The execution is completed, and the barrier has been notified! [Subtask A] The execution is completed, and the barrier has been notified! >>>> The Chief Executive Officer is ready! <Process finished with exit code 0: The execution result shows that when all sub-tasks are completed, the Director executes the tasks and achieves the control goal.

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.