Java Reference
In-Depth Information
A user thread has a life of its own that is not dependent on the thread that creates it. It can continue ex-
ecution after the thread that created it has ended. The default thread that contains main() is a user thread,
as shown in the diagram, but thread3 shown in the diagram could continue to execute after main() has
returned. Threads that run for a finite time are typically user threads, but there's no reason why a daemon
thread can't be finite. Threads that run indefinitely should usually be defined as daemon threads simply be-
cause you need a means of stopping them. A hypothetical example might help you to understand this, so
let's consider how a network server handling transactions of some kind might work in principle.
A network server might be managed overall by a user thread that starts one or more daemon threads to
listen for requests. When the server starts up, the operator starts the management thread, and this thread cre-
ates daemon threads to listen for requests. Each request that is recognized by one of these daemon threads
might be handled by another thread that is created by the listening thread, so that each request is handled
independently. Where processing a transaction takes a finite time, and where it is important that the requests
are completed before the system shuts down, the thread to handle the request might be created as a user
thread to ensure that it runs to completion, even if the listening thread that created it stops. Generally you
would not want a program to be able to create an unlimited number of threads because the more threads
there are running, the greater the operating system overhead there is in managing the threads. For this reas-
on, a program often makes use of a thread pool of a specified fixed number of threads. When a new thread
is required for a particular task, such as servicing a request, one of the threads in the thread pool is alloc-
ated to the task. If all the threads in the pool have been allocated, then a new thread cannot be started until
one of the threads that is currently running terminates. The class libraries provide help in the creation and
management of thread pools through the java.util.concurrent.ThreadPoolExecutor class. When the
time comes to shut the system down, the operator doesn't have to worry about how many listening threads
are running. When the main thread is shut down all the listening threads also shut down because they are
daemon threads. Any outstanding threads dealing with specific transactions then run to completion.
Note that you can call setDaemon() for a thread only before it starts; if you try to do so afterward, the
method throws an IllegalThreadStateException exception. Also, a thread that is itself created by a dae-
mon thread is a daemon by default.
Creating Thread Objects
In the main() method you create three Thread variables that store three different objects of type TryThread .
As you can see, each object has an individual name pair as the first two arguments to its constructor, and a
Search WWH ::




Custom Search