Java Tutorial/Thread/Thread Stop — различия между версиями

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

Версия 17:44, 31 мая 2010

Stopping a Thread: Use boolean value to stop a thread

class CounterThread extends Thread {
  public boolean stopped = false;
  int count = 0;
  public void run() {
    while (!stopped) {
      try {
        sleep(1000);
      } catch (InterruptedException e) {
      }
      System.out.println(count++);
      ;
    }
  }
}
public class MainClass {
  public static void main(String[] args) {
    CounterThread thread = new CounterThread();
    thread.start();
    try {
      Thread.sleep(10000);
    } catch (InterruptedException e) {
    }
    thread.stopped = true;
    System.out.println("exit");
  }
}



0
1
2
3
4
5
6
7
8
9
exit


Stopping a Thread with interrupt()

class TryThread extends Thread {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
    setDaemon(true);
  }
  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 MainClass {
  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);
    }
  }
}



A B C a 
A b 
B a 
A c 
C a 
A b 
B a 
A b 
B c 
a 
C A a 
A b 
B a 
A c 
C b 
B a 
A a 
A b 
B c 
C a 
A b 
B a 
A a 
A b 
B c 
C a 
A b 
B a 
A c 
B b java.lang.InterruptedException: sleep interrupted
C A a java.lang.InterruptedException: sleep interrupted
C c java.lang.InterruptedException: sleep interrupted


Suspend, resume, and stop a thread.

class MyThread implements Runnable {
  Thread thrd;
  boolean suspended;
  boolean stopped;
  MyThread(String name) {
    thrd = new Thread(this, name);
    suspended = false;
    stopped = false;
    thrd.start();
  }
  public void run() {
    try {
      for (int i = 1; i < 10; i++) {
        System.out.print(".");
        Thread.sleep(50);
        synchronized (this) {
          while (suspended)
            wait();
          if (stopped)
            break;
        }
      }
    } catch (InterruptedException exc) {
      System.out.println(thrd.getName() + " interrupted.");
    }
    System.out.println("\n" + thrd.getName() + " exiting.");
  }
  synchronized void stop() {
    stopped = true;
    suspended = false;
    notify();
  }
  synchronized void suspend() {
    suspended = true;
  }
  synchronized void resume() {
    suspended = false;
    notify();
  }
}
public class Main {
  public static void main(String args[]) throws Exception {
    MyThread mt = new MyThread("MyThread");
    Thread.sleep(100);
    mt.suspend();
    Thread.sleep(100);
    mt.resume();
    Thread.sleep(100);
    mt.suspend();
    Thread.sleep(100);
    mt.resume();
    Thread.sleep(100);
    mt.stop();
  }
}





To determine whether a thread has been interrupted

class TryThread extends Thread {
  public TryThread(String firstName, String secondName, long delay) {
    this.firstName = firstName;
    this.secondName = secondName;
    aWhile = delay;
    setDaemon(true);
  }
  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 MainClass {
  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);
    } catch (Exception e) {
      System.out.println(e);
    }
      first.interrupt();
      second.interrupt();
      third.interrupt();
    if (first.isInterrupted()) {
      System.out.println("First thread has been interrupted.");
    }
  }
}



A B C a 
A b 
B a 
A c 
C a 
A b 
B a 
A b 
B c 
C a 
A a 
A b 
B a 
A c 
C b 
B a 
A a 
A b 
B c 
C a 
A b 
B a 
A a 
A b 
B c 
C a 
A b 
B a 
A c 
C C c java.lang.InterruptedException: sleep interrupted
First thread has been interrupted.
B b java.lang.InterruptedException: sleep interrupted
A a java.lang.InterruptedException: sleep interrupted