案例分析

  • 案例1

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class JoinTest1  extends  Thread{


    public void run(){
    System.out.println(Thread.currentThread().getName()+":begin");
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+":end");
    }


    public static void main(String[] args) throws InterruptedException {
    JoinTest1 joinTest1 = new JoinTest1();
    joinTest1.setName("childThread");
    long a = System.currentTimeMillis();
    joinTest1.start();

    joinTest1.join();
    long b = System.currentTimeMillis();

    System.out.println("等待时间:"+(b-a));
    System.out.println("end----------------");

    }
    }

    执行结果:

    1
    2
    3
    4
    5
    6
    childThread:begin
    childThread:end
    等待时间:2003
    end----------------

    Process finished with exit code 0

    主线程等待子线程睡眠2秒钟之后继续执行,结果没有问题

  • 案例2

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class JoinTest2 extends  Thread{


    public void run(){
    System.out.println(Thread.currentThread().getName()+":begin");
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+":end");
    }


    public static void main(String[] args) throws InterruptedException {
    JoinTest2 joinTest2 = new JoinTest2();
    joinTest2.setName("childThread");
    long a = System.currentTimeMillis();
    joinTest2.start();

    joinTest2.join(500);
    long b = System.currentTimeMillis();

    System.out.println("等待时间:"+(b-a));
    System.out.println("end----------------");

    }
    }

    执行结果:

    1
    2
    3
    4
    5
    6
    childThread:begin
    等待时间:501
    end----------------
    childThread:end

    Process finished with exit code 0

    join(long millis)的方法在时间到了之后结束了等待,主线程继续执行

  • 案例3

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    public class JoinTest3 extends  Thread{


    public void run(){
    System.out.println(Thread.currentThread().getName()+":begin");
    try {
    Thread.sleep(2000);
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    System.out.println(Thread.currentThread().getName()+":end");
    }


    public static void main(String[] args) throws InterruptedException {
    JoinTest3 joinTest3 = new JoinTest3();
    joinTest3.setName("childThread");
    long a = System.currentTimeMillis();
    joinTest3.start();

    joinTest3.join(5000);
    long b = System.currentTimeMillis();

    System.out.println("等待时间:"+(b-a));
    System.out.println("end----------------");

    }
    }

    执行结果:

    1
    2
    3
    4
    5
    6
    childThread:begin
    childThread:end
    等待时间:2005
    end----------------

    Process finished with exit code 0