Summary

Here’s a list of the methods of the Thread class that we introduced in this chapter:

Thread()

Constructs a thread object using default values for all options.

Thread(Runnable target)

Constructs a new thread object associated with the given Runnable object.

Thread(String name)

Constructs a thread object with a name that is already assigned. This constructor is used when threading by inheritance.

Thread(Runnable target, String name)

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.

void run()

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.

void start()

Creates a new thread and executes the run() method defined in this thread class.

void stop() (deprecated in Java 2)

Terminates an already running thread.

static void sleep (long milliseconds)

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.

static void sleep (long milliseconds, int nanoseconds)

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.

boolean isAlive()

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.

void join()

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.

void join(long timeout)

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.

void join(long timeout, int nanos)

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.

void setName(String name)

Assigns a name to the Thread instance.

String getName()

Gets the name of the Thread instance.

static Thread currentThread()

Gets the Thread object that represents the current thread of execution. The method is static and may be called through the Thread class name.

static int enumerate(Thread threadArray[])

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.

static int activeCount()

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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset