欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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");  
    }
  }
}