关于java:Java-线程三

43次阅读

共计 817 个字符,预计需要花费 3 分钟才能阅读完成。

退出一个线程

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());
        }
        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
                sleepy = new Sleeper("Sleepy", 1500),
                grumpy = new Sleeper("Grumpy",1500);
        Joiner
                dopey = new Joiner("Dopey", sleepy),
                doc = new Joiner("Doc", grumpy);
        grumpy.interrupt();}
}

正文完
 0