Java Reference
In-Depth Information
Summary
A thread is a path of execution that executes within a process. When
you run a Java program, the main() method runs in a thread. The
main() method can start other threads.
■■
A process terminates when all its non-daemon threads run to
completion.
■■
A thread is created by writing a class that implements
java.lang.Runnable and associating an instance of this class with a new
java.lang.Thread object. The new Thread is initially in the born state.
■■
Invoking the start() method on a Thread object starts the thread and
places it in its appropriate runnable queue, based on its priority.
■■
Java uses a preemptive scheduling mechanism where threads of higher
priority preempt running threads of a lower priority. However, the
actual behavior of threads also relies on the underlying platform.
■■
A thread can also be written by writing a class that extends
java.util.TimerTask and associating an instance of this class with a
java.util.Timer object. This type of thread is useful when performing
scheduled tasks.
■■
The synchronized keyword is used to synchronize a thread on a par-
ticular object. When a thread is synchronized on an object, the thread
owns the lock of the object. Any other thread attempting to synchronize
on this object will become blocked until the object's lock becomes avail-
able again.
■■
Care needs to be taken when synchronizing threads, since deadlock may
occur. Ordering locks is a common technique for avoiding deadlock.
■■
The wait() and notify() methods from the Object class are useful
when multiple threads need to access the same data in any type of
producer/consumer scenario.
■■
Search WWH ::




Custom Search