Here’s a list of the methods of the Thread class that we introduced in this chapter:
Constructs a thread object using default values for all options.
Constructs a new thread object associated with the given Runnable object.
Constructs a thread object with a name that is already assigned. This constructor is used when threading by inheritance.
Constructs a thread object that is associated with the given Runnable object and is created with a name that is already assigned. This constructor is used when threading by interfaces.
The method that the newly created thread will execute. Developers
should override this method with the code they want the new thread to
run; we’ll show the default implementation of the
run()
method a little further on, but it is
essentially an empty method.
Creates a new thread and executes the run()
method
defined in this thread class.
Terminates an already running thread.
Puts the currently executing thread to sleep for the specified number of milliseconds. This method is static and may be accessed through the Thread class name.
Puts the currently executing thread to sleep for the specified number of milliseconds and nanoseconds. This method is static and may be accessed through the Thread class name.
Determines if a thread is considered alive. By definition, a thread is considered alive from sometime before a thread is actually started to sometime after a thread is actually stopped.
Waits for the completion of the specified thread. By definition,
join()
returns as soon as the thread is considered
“not alive.” This includes the case in which the
join()
method is called on a thread that has not
been started.
Waits for the completion of the specified thread, but no longer than the timeout specified in milliseconds. This timeout value is subject to rounding based on the capabilities of the underlying platform.
Waits for the completion of the specified thread, but no longer than a timeout specified in milliseconds and nanoseconds. This timeout value is subject to rounding based on the capabilities of the underlying platform.
Assigns a name to the Thread instance.
Gets the name of the Thread instance.
Gets the Thread object that represents the current thread of execution. The method is static and may be called through the Thread class name.
Gets all the thread objects of the program and stores the result into the thread array. The value returned is the number of thread objects stored into the array. The method is static and may be called through the Thread class name.
Returns the number of threads in the program. The method is static and may be called through the Thread class name.
In this chapter, we have had our first taste of creating, starting, and stopping threads. This is achieved through the methods of the Thread class, which also contains methods that allow us to examine the status of threads, the names of threads, and the threads that our program is using. This provides us with the basics for writing simple, independent threads.
However, there are other issues that must be dealt with when it comes to threads: most notably, that communication between the individual threads must avoid the race conditions we outlined. This issue of communication, or synchronization, will be discussed in the next chapter.