码迷,mamicode.com
首页 > 编程语言 > 详细

Java多线程之join

时间:2014-05-25 22:17:16      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   c   code   java   

将另外一个线程join到当前线程,则需要等到join进来的线程执行完才会继续执行当前线程。

bubuko.com,布布扣
package Thread.join;

class Sleeper extends Thread {
    private int duration;

    public Sleeper(String name, int sleepTime) {
        super(name);
        duration = sleepTime;
        start();
    }

    public void run() {
        try {
            sleep(duration);
        } catch (InterruptedException e) {
            System.out.println(getName() + " was interrupted."
                    + "isInterrupted():" + isInterrupted());
            return;
        }
        System.out.println(getName() + " has awakened");
    }
}

class Joiner extends Thread {
    private Sleeper sleeper;

    public Joiner(String name, Sleeper sleeper) {
        super(name);
        this.sleeper = sleeper;
        start();
    }

    public void run() {
        try {
            sleeper.join();
        } catch (InterruptedException e) {
            System.out.println("Interrupted");
        }
        System.out.println(getName() + " join completed");
    }
}

public class Joining {
    public static void main(String[] args) {
        Sleeper sleepy01 = new Sleeper("Sleepy01", 1500), sleepy02 = new Sleeper(
                "Sleepy02", 1500);
        Joiner joiner01 = new Joiner("Joiner01", sleepy01), joiner02 = new Joiner("Joiner02",
                sleepy02);
        joiner01.interrupt();

    }
}
bubuko.com,布布扣

 

Java多线程之join,布布扣,bubuko.com

Java多线程之join

标签:style   class   blog   c   code   java   

原文地址:http://www.cnblogs.com/zhuawang/p/3751209.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!