26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 15 Multithreading 869<br />

48<br />

49 } // end class SharedCell<br />

Fig. Fig. 15.16 15.16 Threads modifying a shared array of cells (part 2 of 2).<br />

15.9 Daemon Threads<br />

A daemon thread is a thread that runs for the benefit of other threads. Unlike conventional<br />

user threads (i.e., any non-daemon thread in a program), daemon threads do not prevent a<br />

program from terminating. The garbage collec<strong>to</strong>r is a daemon thread. Nondaemon threads<br />

are conventional user threads or threads such as the event-dispatch thread used <strong>to</strong> process<br />

GUI events. We designate a thread as a daemon with the method call<br />

setDaemon( true );<br />

A false argument means that the thread is not a daemon thread. A program can include<br />

a mixture of daemon threads and nondaemon threads. When only daemon threads remain<br />

in a program, the program exits. If a thread is <strong>to</strong> be a daemon, it must be set as such before<br />

its start method is called or an IllegalThreadStateException is thrown.<br />

Method isDaemon returns true if a thread is a daemon thread and false otherwise.<br />

Common <strong>Program</strong>ming Error 15.4<br />

Starting a thread, then attempting <strong>to</strong> make the thread a daemon thread, causes an IllegalThreadStateException.<br />

15.4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!