Java Reference
In-Depth Information
Thread simplestThread = new Thread( );
simplestThread.start( );
simplestThread
object in memory
Gets CPU time
Starts executing the run( ) method
of the Thread class
Created a thread and
scheduled it for execution
The run( ) method finishes
Thread is dead
Figure 6-4. The simplest thread execution
There are two important points to add to the current discussion.
When a thread is dead, it does not mean the thread object is garbage collected. Note that
a thread is a unit of execution. “A thread is dead” means that the unit of execution that the
thread represented has finished its work. However, the thread object representing the unit of
execution still exists in memory. After the thread is dead, the object will be garbage collected
based on the same garbage collection rules that are used for any other Java objects. Some
restrictions exist that dictate the methods you can call on a dead thread. For example, you
cannot call its start() method again. That is, a thread object can be started only once.
However, you can still check if the thread is dead by calling the isAlive() method of the
thread object.
run() method in one go. Rather, the
operating system decides the amount of time to allocate and when to allocate that time to
the thread.
The thread does not get the CPU time to execute
Specifying Your Code for a Thread
There are three ways you can specify your code to be executed by a thread:
Thread class
By inheriting your class from the
Runnable interface in your class
By implementing the
void
By using the method reference to a method that takes no parameters and returns
 
 
Search WWH ::




Custom Search