Thrad Life
Thrad Life
new state
runnable state
running state
blocked state
dead state/ terminated
new state:- The thread comes to this stage after creating the instance of thread
but before the execution of start method
new
start( ) stop( )
Runnable Dead
Runnable state:- The runnable state means that the thread is ready for
execution and is waiting for the availability of the processor.
If all thread have equal priority, then they are given time slots for execution
in round robin fashion i.e. first come first serve manner.
Running state:- Running means that the processor has given its time to the
thread for its execution.
a) suspend( ): This approach is useful when we want to suspend a thread for some
time due to certain reason, but do not want to kill it.
b) sleep( ): It has been made to sleep. We can put a thread to sleep for a specified
time period using the method sleep(time), where time is in milliseconds.
c) wait( ): It has been told to wait until some events occurs.
A blocked thread is considered “not runnable” but not dead and therefore
fully qualified to run again.
Dead state/Terminated:- The thread comes to this stage when run method
is terminated or completed.
Newborn
start( )
stop( )
run( )
stop ( )
Running
Dead
Runnable
yield( )
suspend( ) stop ( )
resume( )
sleep( ) notify( )
wait( )
Blocked
Creating threads:-
Threads are implemented in the form of object that contain a method called run( )
method. The run( ) method is the heart and soul for any thread. It makes up entire
body of a thread and is the only method in which different behavior can be
implemented.
example:-
----------------------
---------------------- main thread
---------------------- module
start
switching
---------------- ----------------- -----------------
---------------- ----------------- -----------------
---------------- ----------------- -----------------
fig: Multithreaded
Priority in multithreading:-
1. MAX – PRIORITY = 6 – 10
2. NORM – PRIORITY = 5
3. MIN – PRIORITY = 1 – 4
1. Inheritance option: If we extend Thread class then we cannot extend any other
class.
If we implement Runnable interface then the Runnable interface will provide
the choice to extend any other class.
example:-
(thread)
class cls1 extends Thread
{
-----------------
}
(Runnable)
class cls2 implements Runnable extends Applet
{
------------------
}
(thread)
class class_name extends Thread
{
public void run( )
{
statements
}
}
(Runnable)
class class_name implements Runnable
{
public void run( )
{
statements
}
}
Problem in multithreading:-
deadlock
synchronization
class share
{
synchronized public void show(string str)
{
System.out.print(“[”+str);
try
{
Thread.sleep(1000);
}
catch (Exception e)
{
}
System.out.println(“]”);
}
}
class cls1 extends Thread
{
Share obj;
String str;
cls1(Share ob, String name)
{
obj = ob;
str = name;
start( );
}
public void run( )
{
obj.show(str);
}
}
class cls2 extends Thread
{
Share obj;
String str;
cls2(Share ob, String name)
{
obj = ob;
str = name;
start( );
}
public void run( )
{
obj.show(str);
}
}
public class abc
{
public static void main(String args[])
{
Share O = new Share( );
cls1 obj1 = new cls1(O, “first Thread”);
cls2 obj2 = new cls2(O, “second Thread”);
}
}