Thead join sample code
程序员文章站
2022-07-14 12:48:02
...
public class Join {
public static void main (String [] args) throws Exception {
Controller c = new Controller();
c.start();
Thread.sleep(100);
c.interrupt();
}
}
class Job extends Thread {
@Override
public void run() {
try {
Thread.sleep(2000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}
class Controller extends Thread {
@Override
public void run() {
try {
Job j = new Job();
j.start();
j.join(1000);
if (j.isAlive())
System.out.println("job is still alice");
} catch (InterruptedException ex) {
System.out.println("interrupteded while joning");
}
}
}
下一篇: gpg使用方法