|
||||||
|
A thread is a single sequence of executed code. Java supports multi-threaded applications and applets that can run many threads simultaneously, within the abilities of the underlying operating system to run things anything simultaneously. Threads are not objects. Threads can run in and out of objects and many threads can be be running a given method or accessing a given property at the same time. This fact is very important and is the root of most of the issues involved with inter-thread interactions. For instance, the programmer must be very concerned with what happens if more than one thread is trying to change or access a piece of data at the same time ("synchronization"). Or what happens if when threads are waiting for eachother ("racing" and "locking"). A full discussion of these issues are beyond the scope of this web page, though there is a brief discussion below. There are two ways to create ("spawn" or "fork") a thread is in Java: subclassing the Thread class or using the Runnable interface: The Thread ClassThe Thread class is not to be confused with the thread itself. The Thread is an encapsulation of the thread construction and gives a way of communicating with the thread as an entity. The Thread class is used to spawn the thread and can be used to start or pause the thread. The Thread class has three main methods:
To spawn a new thread using the Thread class:
The Runnable InterfaceThe Runnable interface specifies only one method, which takes no parameters:
This method effectively replaces the run() method of the Thread class and frees the programmer from the constraints of having to subclass Thread. A Thread object must still be made, as its start() method is still needed. To spawn a thread using the Runnable interface:
Thread Synchronization IssuesThe Volatile KeywordThis keyword is used in front of a property declaration to indicate to the compiler to never assume that it knows the value of this property and to always reread its value. The volatile keyword is needed when one thread changes the value of a property and another thread reads it. The Synchronized KeywordThis keyword is used in front of a method declaration to prevent multiple threads from simultaneously executing the method. If one thread is executing the method, then all other threads are "locked" out and wait for the first method to finish before they get their turn. A full discussion of thread scheduling is beyond the scope of this web page. The synchronized keyword is needed when the method requires that only one thread be active at a time, for instance, when all the threads use the method to update the value of a single property and that value is dependent on the values from all the threads, e.g. a sum. The Object.wait() methodThis method, inherited by all Objects, causes the current thread to immediately pause and wait indefinitely for a notify command. This method is used to synchronize multiple threads. The Object.notify() methodThis object is called to restart a waiting thread. |