Join in Java

Source: Internet
Author: User

You have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2? You have three threads T1, T2, and T3. How can you run T2. after T1. after T2. after T2? The following is a possible implementation method:

public class ThreadJoin implements Runnable {            private final Thread joinTarget;      public ThreadJoin(Thread joinTarget) {          this.joinTarget = joinTarget;      }            @Override      public void run() {          if(joinTarget != null) {              try {                  joinTarget.join();              } catch (InterruptedException e) {                  e.printStackTrace();              }                            System.out.println("Join complete " + Thread.currentThread().getName());          }      }            private static class MyThread implements Runnable {          @Override          public void run() {              System.out.println("MyThread");          }      }               public static void main(String[] args) {          Thread t1 = new Thread(new MyThread(), "Thread1");          Thread t2 = new Thread(new ThreadJoin(t1), "Thread2");          Thread t3 = new Thread(new ThreadJoin(t2), "Thread3");          t1.start();          t2.start();          t3.start();      }  }  

 

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.