Thread: interrupt() : Thread « java.lang « Java by API






Thread: interrupt()

  
class TryThread extends Thread {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
  }
  public void run() {
    try {
      while (true) {
        System.out.print(firstName);
        sleep(aWhile);
        System.out.print(secondName + "\n");
      }
    } catch (InterruptedException e) {
      System.out.println(firstName + secondName + e);
    }
  }
  private String firstName;
  private String secondName;
  private long aWhile;
}
public class Main {
  public static void main(String[] args) {
    Thread first = new TryThread("A ", "a ", 200L);
    Thread second = new TryThread("B ", "b ", 300L);
    Thread third = new TryThread("C ", "c ", 500L);
    first.start();
    second.start();
    third.start();
    try {
      Thread.sleep(3000);
      first.interrupt();
      second.interrupt();
      third.interrupt();
    } catch (Exception e) {
      System.out.println(e);
    }
  }
}
  

   
    
  








Related examples in the same category

1.Thread.MAX_PRIORITY
2.Thread.NORM_PRIORITY
3.new Thread(Runnable target, String name)
4.Thread.activeCount()
5.Thread.currentThread()
6.Thread.dumpStack()
7.Thread.enumerate(Thread[] tarray)
8.Thread: getStackTrace()
9.Thread: getThreadGroup()
10.Thread: getUncaughtExceptionHandler()
11.Thread: isAlive()
12.Thread: isDaemon()
13.Thread: join() (Using join() to wait for threads to finish)
14.Thread: run()
15.Thread: setDaemon(boolean b)
16.Thread: setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
17.Threah.sleep(long millis)
18.Thread: setDaemon(boolean on)
19.Thread: setName(String name)
20.Thread: setPriority(int newPriority)
21.Thread: setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
22.Thread: start
23.Thread: stop
24.implements UncaughtExceptionHandler