Professional Documents
Culture Documents
|
Threads vs. Processes
A computer is composed of several distinct parts one of which is the
Central Processing Unit (CPU)
When a program is executing, each of the instructions that compose the
program is fetched from memory into the CPU for processing. This
program in execution is called a process.
It is usually said that a modern computer can process many processes
concurrently. This is called multi-processing. In reality this means that
many processes are in various states of execution in any single instant.
However when a computer has a single CPU, then in any given instant,
only a single instruction can be executed. Thus true multi-processing can
only be achieved when a computer has more than one processor.
On a single CPU computer, the fact that many processes are in various
states of execution at a single instant is known as multi-programming.
Each process has its own variables.
|
Threads vs. Processes
The ability of a single process to spawn multiple execution paths is
called multi-threading. Each path is called a thread. A thread is also
referred to as a lightweight process. Unlike a process, each thread
shares the same set of data (variables). If two threads access this data
at the same time, synchronization problems can occur.
Generally, multi-threading is a blessing since it allows the same program
to handle multiple events ´concurrentlyµ
When threads are used: a process can e.g.:
² Load an image while it is making a calculation
² Garbage-collect behind the scenes while executing the main path of
the program
² Send data over a network while it is filling a rectangle
New thread can be created by instantiating a ë object as follows:
ë
ë
where m is an object from a class which implements the
and as such must provide a method.
|
Thread Creation by Implementing Runnable
A thread is a virtual program in execution. When you create a new
thread, you are creating a context for a program, which includes data,
memory, and a CPU. The thread will execute the method for
whichever object is passed to the ë constructor. The data for
this execution context is the data for the
.
The above code simply creates a thread. To begin the runnable lifetime
of a thread, you must call the following method from the Thread class:
The entire process is shown below:
Class MyObject implements Runnable {
// perhaps some data here
public MyObject() { }
public void run() { System.out.println(´got hereµ); } }
public class TestMyObject {
public static void main(String args[]) {
MyObject m = new MyObject();
Thread t = new Thread(m);
t.start(); }
}
|
Thread Creation by Implementing Runnable
When you call the method of the ë class, the JVM
performs some necessary initialization for this thread and then calls
the method of the threaded object.
The result is that two threads are running concurrently:
² The main thread
² And the other thread, which is executing in the method
Soon we will discuss another way of creating a thread using subclassing.
In those cases, subclasses of ë should override the ë
method.
Once the method has been called, the method is called by
, and the ë is off and running, executing the code inside the
method. When the method terminates, the thread is dead.
|
Creating Threads by Subclassing
The Oë class implements the interface. Thus
another way to create a thread is to create a subclass of ë and
override the method.
|
Thread States
A thread can be in various states during its lifetime:
Various thread methods control the state in which the thread exists.
We have seen a few of these methods such as
and
methods.
Other methods of the
class are:
V
VVV
causes the currently executing thread object to tempoarily pause
and allow other threads to execute.
VV
V
V
VV
sets the priority of this thread to the new priority ² newpr
V
VV VV
V
causes the currently executing thread to sleep for the specified
number of milliseconds
V
V
returns a reference to the currently executing thread object
|
Thread States
VV
V
returns this thread·s name
VV
V V
V
waits for this thread to die
VV
V!V
tests if this thread is alive; a thread is alive if it has been started
and has not yet died
|