Overview

Chapter 5 - Gracefully Stopping Threads

Java Thread Programming
Paul Hyde
  Copyright 1999 Sams Publishing

Daemon Threads
Threads that are marked as daemons stop in a whole new way. Daemon threads are used for background supporting tasks and are only needed while normal, nondaemon threads are still running. When the VM detects that the only remaining threads are daemon threads, it exits. If any non daemon thread is still alive , the VM will not exit. Daemon threads provide a nice way of managing some sort of behind-the-scenes processing that is only necessary to support other nondaemon threads.
The classes DaemonThread (see Listing 5.12) and DaemonThreadMain (see Listing 5.13 ) show how a daemon thread behaves.
Listing 5.12  DaemonThread.javaThe Daemon Thread Example
1: public class DaemonThread extends Object implements Runnable {
2:     public void run() {
3:         System.out.println(entering run());
4:
5:         try {
6:             System.out.println(in run() - currentThread()= +
7:                     Thread.currentThread());
8:
9:             while (true) {
10:                 try { Thread.sleep(500); }
11:                 catch (InterruptedException x) {}
12:
13:                 System.out.println(in run() - woke up again);
14:             }
15:         } finally {
16:             System.out.println(leaving run());
17:         }
18:     }
19: }
In this simple example, DaemonThread has only one method: run() . The message entering run() is printed when a thread begins (line 3). The while loop (lines 914) runs indefinitely, printing the woke up again message twice each second.
Listing 5.13  DaemonThreadMain.javaThe Entry Point for the Daemon Thread Example
1: public class DaemonThreadMain extends Object {
2:     public static void main(String[] args) {
3:         System.out.println(entering main());
4:
5:         Thread t = new Thread(new DaemonThread());
6:         t.setDaemon(true);
7:         t.start();
8:
9:         try { Thread.sleep(3000); } catch (InterruptedException x) { }
10:
11:         System.out.println(leaving main());
12:     }
13: }
In DaemonThreadMain , a new DaemonThread object is created and passed to one of Thread s constructors (line 5). Before the new thread is started, the daemon flag is set to true (line 6) to indicate that this thread should not keep the VM from exiting. Next, the thread is started (line 7). After letting the daemon thread run for three seconds (line 9), the main thread simply falls out of main() and dies. Very soon after this, the VM notices that the other thread is only a daemon thread and exits.
When DaemonThreadMain is run, something close to the following should be produced (your output might differ ):
entering main()
entering run()
in run() - currentThread()=Thread[Thread-0,5,main]
in run() - woke up again
in run() - woke up again
in run() - woke up again
in run() - woke up again
in run() - woke up again
leaving main()
in run() - woke up again
One last woke up again message might be printed after the leaving main message. If the new thread was not a daemon thread, the woke up again messages would continue to be produced until the VM was terminated manually. Also notice that the daemon thread was abruptly stopped and never printed the message in its finally clause.
  Caution Although daemon threads can be very useful, care must be taken to ensure that its not harmful for them to be stopped in their tracks when all the other nondaemon threads have died.

Toc


Java Thread Programming
Java Thread Programming
ISBN: 0672315858
EAN: 2147483647
Year: 2005
Pages: 149
Authors: Paul Hyde

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net