Java Reference
In-Depth Information
low-priority threads run while there are high-priority threads waiting. In most
cases, there is no way to influence how the scheduler will interpret priorities.
Thread priorities are represented as an integer between 1 and 10.
setName() and getName()
Allows the developer to set or retrieve a name for an individual thread. Naming
threads is good practice, as it can make debugging much easier, especially when
using a tool such as jvisualvm , which we will discuss in “VisualVM” on page 362 .
getState()
Returns a Thread.State object that indicates which state this thread is in, as per the
values defined in “Thread Lifecycle” on page 209 .
isAlive()
Used to test whether a thread is still alive.
start()
This method is used to create a new application thread, and to schedule it, with the
run() method being the entry point for execution. A thread terminates normally
when it reaches the end of its run() method or when it executes a return statement
in that method.
interrupt()
If a thread is blocked in a sleep() , wait() , or join() call, then calling interrupt()
on the Thread object that represents the thread will cause the thread to be sent an
InterruptedException (and to wake up). If the thread was involved in interruptible
I/O then the I/O will be terminated and the thread will receive a ClosedByInterrup
tException . The interrupt status of the thread will be set to true, even if the thread
was not engaged in any activity that could be interrupted.
join()
The current thread waits until the thread corresponding to the Thread object has
died. It can be thought of as an instruction not to proceed until the other thread has
completed.
setDaemon()
A user thread is a thread that will prevent the process from exiting if it is still alive—
this is the default for threads. Sometimes, programmers want threads that will not
prevent an exit from occurring—these are called daemon threads . The status of a
thread as a daemon or user thread can be controlled by the setDaemon() method.
Search WWH ::




Custom Search