多线程之Thread的join方法

1.前言

在java编程中多线程编程一直时一个重点和难点,今天分享一个多线程知识中的小点,关于Thread.join()方法。

2.示例代码

例子1

public class Main {

    public static void main(String[] args) {
        MainThread mainThread = new MainThread();
        JoinThread joinThread = new JoinThread(mainThread);
    }
}

class MainThread extends Thread{

    public MainThread(){
        start();
    }

    @Override
    public void run() {
        System.out.println("MainThread is start-("+Thread.currentThread().getName()+")");
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("MainThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("MainThread sleep is interrupt");
            }
        }
        System.out.println("MainThread is over");
    }
}

class JoinThread extends Thread{

    private Thread mainThread;

    public JoinThread(Thread thread){
        this.mainThread = thread;
        start();
    }

    @Override
    public void run() {
        System.out.println("JoinThread is start-("+Thread.currentThread().getName()+")");
        try {
            mainThread.join();
        } catch (InterruptedException e) {
            System.out.println("JoinThread join is interrupt");
        }
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("JoinThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("JoinThread sleep is interrupt");
            }
        }
        System.out.println("JoinThread is over");
    }
}

输出:

MainThread is start-(Thread-0)
MainThread loop #0
JoinThread is start-(Thread-1)
MainThread loop #1
MainThread loop #2
MainThread loop #3
MainThread loop #4
MainThread is over
JoinThread loop #0
JoinThread loop #1
JoinThread loop #2
JoinThread loop #3
JoinThread loop #4
JoinThread is over

从结果上可以看出,明明是两个线程,但是做到了顺序执行。由此可以得出结果:join的作用是加入线程的执行队列,前一个线程执行完成后,后一个线程才能执行。上面的例子中,mainthread先执行,然后jointhread加入,但是发现mainthread还在执行,就挂起自己等待mainthread执行完毕,再执行自己。

例子2

public class Main {

    public static void main(String[] args) {
        MainThread mainThread = new MainThread();
        JoinThread joinThread = new JoinThread(mainThread);
    }
}

class MainThread extends Thread{

    public MainThread(){
        start();
    }

    @Override
    public void run() {
        System.out.println("MainThread is start-("+Thread.currentThread().getName()+")");
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("MainThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("MainThread sleep is interrupt");
            }
        }
        System.out.println("MainThread is over");
    }
}

class JoinThread extends Thread{

    private Thread mainThread;

    public JoinThread(Thread thread){
        this.mainThread = thread;
        start();
    }

    @Override
    public void run() {
        System.out.println("JoinThread is start-("+Thread.currentThread().getName()+")");
        try {
            mainThread.join(3000);
        } catch (InterruptedException e) {
            System.out.println("JoinThread join is interrupt");
        }
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("JoinThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("JoinThread sleep is interrupt");
            }
        }
        System.out.println("JoinThread is over");
    }
}

结果:

MainThread is start-(Thread-0)
MainThread loop #0
JoinThread is start-(Thread-1)
MainThread loop #1
MainThread loop #2
JoinThread loop #0
MainThread loop #3
JoinThread loop #1
MainThread loop #4
JoinThread loop #2
MainThread is over
JoinThread loop #3
JoinThread loop #4
JoinThread is over

例子2跟例子1的区别就一行代码mainThread.join(3000),这次给join方法加上了参数,其效果通俗的讲就是jointhread加入执行队列,但是就等了3秒,就退出了执行队列,自己执行任务去了。

例子3

public class Main {

    public static void main(String[] args) {
        MainThread mainThread = new MainThread();
        JoinThread joinThread = new JoinThread(mainThread);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                mainThread.interrupt();
            }
        },2000);
    }
}

class MainThread extends Thread{

    public MainThread(){
        start();
    }

    @Override
    public void run() {
        System.out.println("MainThread is start-("+Thread.currentThread().getName()+")");
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("MainThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("MainThread sleep is interrupt");
                break;
            }
        }
        System.out.println("MainThread is over");
    }
}

class JoinThread extends Thread{

    private Thread mainThread;

    public JoinThread(Thread thread){
        this.mainThread = thread;
        start();
    }

    @Override
    public void run() {
        System.out.println("JoinThread is start-("+Thread.currentThread().getName()+")");
        try {
            mainThread.join();
        } catch (InterruptedException e) {
            System.out.println("JoinThread join is interrupt");
        }
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("JoinThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("JoinThread sleep is interrupt");
                break;
            }
        }
        System.out.println("JoinThread is over");
    }
}

结果:

MainThread is start-(Thread-0)
MainThread loop #0
JoinThread is start-(Thread-1)
MainThread loop #1
MainThread loop #2
MainThread sleep is interrupt
MainThread is over
JoinThread loop #0
JoinThread loop #1
JoinThread loop #2
JoinThread loop #3
JoinThread loop #4
JoinThread is over

这个例子设置了一个定时器让mainthread被打断,不能正常执行完。发现只要mainthread不存活了(isAlve()返回为假),jointhread就会马上接着执行

例子4

public class Main {

    public static void main(String[] args) {
        MainThread mainThread = new MainThread();
        JoinThread joinThread = new JoinThread(mainThread);
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                joinThread.interrupt();
            }
        },2000);
    }
}

class MainThread extends Thread{

    public MainThread(){
        start();
    }

    @Override
    public void run() {
        System.out.println("MainThread is start-("+Thread.currentThread().getName()+")");
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("MainThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("MainThread sleep is interrupt");
                break;
            }
        }
        System.out.println("MainThread is over");
    }
}

class JoinThread extends Thread{

    private Thread mainThread;

    public JoinThread(Thread thread){
        this.mainThread = thread;
        start();
    }

    @Override
    public void run() {
        System.out.println("JoinThread is start-("+Thread.currentThread().getName()+")");
        try {
            mainThread.join();
        } catch (InterruptedException e) {
            System.out.println("JoinThread join is interrupt");
        }
        for (int i = 0 ; i < 5 ; i++){
            System.out.println("JoinThread loop #"+i);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                System.out.println("JoinThread sleep is interrupt");
                break;
            }
        }
        System.out.println("JoinThread is over");
    }
}

结果:

MainThread is start-(Thread-0)
MainThread loop #0
JoinThread is start-(Thread-1)
MainThread loop #1
MainThread loop #2
JoinThread join is interrupt
JoinThread loop #0
MainThread loop #3
JoinThread loop #1
JoinThread loop #2
MainThread loop #4
JoinThread loop #3
MainThread is over
JoinThread loop #4
JoinThread is over

从结果上看,jointhread挂起等待的行为也会打断,打断后jointhread退出执行队列,自己完成任务

3.总结

java1.5以后,java.util.concurrent.CyclicBarrier这个类可能比最初的线程类库中的join()更加合适,以后我们来探讨CyclicBarrier的使用

你可能感兴趣的:(多线程之Thread的join方法)