11

Stopping a Thread

The ThreadStopping program starts two Threads: a Runner and a Killer. The Runner prints a counter in a loop. The Killer chases the Runner for a given time before it stops it.

  1. public class ThreadStopping {
  2.     private Runner runner;
  3.  
  4.     private Killer killer;
  5.  
  6.     private static final int DELAY = 1 * 1000;
  7.  
  8.     ThreadStopping() {
  9.         runner = new Runner("Runner");
  10.         killer = new Killer("Killer");
  11.     }
  12.  
  13.     public void start() {
  14.         runner.start();
  15.         killer.start();
  16.     }
  17.  
  18.     public static void main(String[] args) {
  19.         (new ThreadStopping()).start();
  20.     }
  21.  
  22.     private class Runner extends Thread {
  23.         private volatile boolean halted = false;
  24.  
  25.         Runner(String s) {
  26.             super(s);
  27.         }
  28.  
  29.         public void run() {
  30.             setPriority(MAX_PRIORITY);
  31.             for (int i = 0; !halted; i++)
  32.                 System.out.println(getName() + " - " + i);
  33.         }
  34.  
  35.         public void halt() {
  36.             halted = true;
  37.         }
  38.     }
  39.  
  40.     private class Killer extends Thread {
  41.         Killer(String s) {
  42.             super(s);
  43.         }
  44.  
  45.         public void run() {
  46.             long now = System.currentTimeMillis();
  47.             long elapsed = 0;
  48.  
  49.             while (elapsed < DELAY) {
  50.                 elapsed = System.currentTimeMillis() - now;
  51.                 System.out.println(getName() + " - " + elapsed);
  52.                 yield();
  53.             }
  54.             System.out.println(getName() + " - " + elapsed);
  55.             System.out.println(getName() + " - " + "Killing " + runner.getName());
  56.             runner.halt();
  57.         }
  58.     }
  59. }
$ javac ThreadStopping.java
$ java ThreadStopping
Killer - 0
Runner - 0
Runner - 1
Runner - 2
...
Runner - 15
Killer - 3
Killer - 5
...
Runner - 107393
Runner - 107394
Killer - 1154
Killer - Killing Runner
Runner - 107395
Runner - 107396

The output of the program shows that the Runner and the Killer are both running alternatively. As soon as the Killer has run more than 1 second, it kills the Runner. Messages from the Runner continue to appear after it has stopped because the output buffer is flushed. Add a call to System.out.flush() after printing messages to flush the output immediately. The program will be slower. You might still see one message after the Killer has killed the Runner if the Runner was interrupted in its loop before testing the flag halted.

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].