chapter 4-Multithreaded Programming (1)
chapter 4-Multithreaded Programming (1)
Introduction
Thread
Thread is a lightweight components and it is a flow of control. In other words
a flow of control is known as thread.
Multithreading in Java
Multithreading in java is a process of executing multiple threads
simultaneously. The aim of multithreading is to achieve the concurrent
execution.
--
}
Step 2. Override run( ) method of Thread class in the above class. Write the
code which is to be executed by the newly created thread in the run( )
method.
Step 3. Create an object for the above class and call start( ) method for the
created object.
(One can call the start( ) method in the constructor of newly created class for
invoking the run( ) method automatically on creation of object of this class)
From creation of thread to its termination, thread follows a cycle known as life
cycle of thread. It is shown in figure 4.5.
Newborn State:
On creation, a thread enters in Newborn state. In this state Thread can call two
methods
Newborn State
start() stop()
Multiple threads may be in Runnable state. The threads in this state are ready
for execution and are waiting for availability of processor. (Processor can run
only one thread at an instance). Selection of a thread for allocation of processor
may be done on the basis of priority of the threads. If threads are having same
priority, threads may get selected on First Come First Serve basis.
Running State:
There can be only one thread in Running state at given instance of time.
Processor is allocated to such thread and it is being run by processor. Running
thread may invoke yield( ) method for giving control to another thread. On
invoking suspend( ) method, thread gets suspended. On invoking sleep(time)
method, thread sleeps for specified number of milliseconds. On invoking wait( )
method, threads goes in waiting. In all these cases, thread enters in Blocked
state.
A B C D
Yield()
Running Thread Runnable Thread
sleep(time)
Running Suspend()
Blocked
State State
wait()
Blocked State:
wait()
Runnable Blocked
State state
notify()
suspend()
Runnable Blocked
State resume() state
sleep()
Runnable Blocked
State state
Dead State:
Thread may enter this state on its natural death. Thread in any state may invoke
stop( ) method for entering in this state.
4.2.3 Thread Exceptions
From creation of thread to its termination, thread follows a cycle known as life
cycle of thread. It is shown in figure 4.5. It is also discussed previously that
various methods may be invoked by threads depending on their state. If a thread
tries to invoke a method which cannot be handled in the current state of that
thread, IllegalThreadStateException is thrown.
Example 1:
threadname.setPriority(intvalue);
This value may be set with the help of different priority constants available
with Thread class.
MAX_PRIORITY = 10
Thread class also supports a getPriority( ) method which returns current priority
value of the thread.
When a high priority thread enters in runnable state while a low priority thread
is in running state, the running thread gets preempted. i.e. the low priority
running threads stops its execution by entering runnable state and the new high
priority thread starts its running by entering running state. This concept is called
preemption. An example program showing preemption is shown below.
notify( ) It resumes the first thread that went to sleep mode. Its syntax is as
follows
final void notify( )
notifyall( ) It resumes all the threads those are in sleep mode. Its syntax is as
follows
final void notifyall( )
wait( ) It sends the calling thread into sleep mode. This thread can come out of
sleep mode only by notify( ) method or notifyall( ) method. Its syntax is as
follows
final void wait( )
--
Write the code which is to be executed by the newly created thread in the run( )
method.
public void run( )
3. Create an object for the above class, Create Thread object by passing
the object just created as parameter to constructor and call start( ) method
for the thread object.
Example:
class MyThread2 implements Runnable
{
public void run( )
{
int i;
System.out.println(“Starting MyThread2”);
for(i=10;i>=1;i--)
{
System.out.println(i*i*i); //Displays cubes from 10 to 1
}
System.out.println(“Exiting MyThread2”);
}
}
class RunnableEx3
{
public static void main(String[] args)
{
System.out.println(“Starting main thread”);
MyThread2 mt = new MyThread2( );
Thread t = new Thread(mt);
t.start( );
System.out.println(“Exiting main thread”);
}
}
Example : Write a program to discuss all the life cycle method of Thread
class