img
. . . . . . .
There is nothing stopping you from starting the thread in the same line as the constructor (Code
Example 4-11); we just don't do that very often.
Example 4-11 Construct and Start in a Single Line
new MyThread().start();
APIs Used in This Chapter
The Class java.lang.Thread
The class Thread defines thread objects. When the start() method is called, an actual running
thread is created which the Thread object can control. It is important to distinguish between the
object (which is just memory and a set of methods) and the running thread (which executes code).
All static thread methods apply to the current thread.
Thread
public
Thread()
public
Thread(String name)
public
Thread(Runnable runObj)
public
Thread(Runnable runObj, String name)
throws SecurityException,
IllegalThreadStateException
These create a new thread object.
References:
Chapters 4 and 10.
start
public void start()
throws IllegalThreadStateException
Calling the start() method on an instance of Thread will cause the appropriate run() method
to execute in a new thread.
Chapter 4.
Reference:
run
public void run()
This is the method you define that actually executes the code you want. The base method simply
looks to see if there is a Runnable and calls its run() method.
Reference:
Chapter 4.
currentThread
public static Thread currentThread()
This method returns the current thread object.
Reference:
Chapter 4.
Search WWH :
Custom Search
Previous Page
Multithreaded Programming with JAVA - Topic Index
Next Page
Multithreaded Programming with JAVA - Bookmarks
Home