Java by API/java.lang/InterruptedException

Материал из Java эксперт
Перейти к: навигация, поиск

InterruptedException as a type

/*
 * Output:
A
A
B
A
 */
class MyThread4 implements Runnable {
  String str;
  long msec;
  MyThread4(String str, long msec) {
    this.str = str;
    this.msec = msec;
    new Thread(this).start();
  }
 
  public void run() {
    try {
      while(true) {
        Thread.sleep(msec);
        System.out.println(str);
      }
    }
    catch(InterruptedException ex) {
      ex.printStackTrace();
    }
  }
}
public class MainClass {
  public static void main(String args[]) {
    
    MyThread4 ta = new MyThread4("A", 1000);
    MyThread4 tb = new MyThread4("B", 3000);
  }
}