0% found this document useful (0 votes)
39 views12 pages

Thrad Life

A thread is a process that can be executed independently or concurrently with other threads. The life cycle of a thread includes new, runnable, running, blocked, and dead states. Creating threads involves defining a run method and starting the thread. Multithreaded programming allows multiple threads to run concurrently by switching between threads.

Uploaded by

Vineet Pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views12 pages

Thrad Life

A thread is a process that can be executed independently or concurrently with other threads. The life cycle of a thread includes new, runnable, running, blocked, and dead states. Creating threads involves defining a run method and starting the thread. Multithreaded programming allows multiple threads to run concurrently by switching between threads.

Uploaded by

Vineet Pathak
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Thread

It is the process that executes independently or along with other threads


to execute a task.

Life cycle of a thread

 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

 schedule it for running using start( )


 kill it using stop( )

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.

Blocked state:- A thread is said to be “blocked” when it is prevented from


entering into the runnable state and subsequently the running state.

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.

A run method can be appear as


public void run( )
{
Statements for implementing thread
}
initiate the run( ) by the help of another thread method called start( )

A new thread can be created in two ways


1. By creating a thread class
Define a class that extends Thread class and override its run( ) method with
the code required by the thread.

example:-

class abc extends thread


{
public void run( )
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(“*”);
}
System.out.println( );
}
}
}
class xyz extends Thread
{
public void run( )
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=I;j++)
{
System.out.print(“$”);
}
System.out.println( );
}
}
}
public class cls
{
public static void main(String args[])
{
abc obj1 = new abc( );
xyz obj2 = new xyz( );
obj1.start( );
obj2.start( );
}
}

Multithreaded programming:- Multithreading is a conceptual programming


paradigm where a program (process) is divided into two or more subprograms
(processes), which can be implemented at the same time in parallel.
main thread

----------------------
---------------------- main thread
---------------------- module

start

switching
---------------- ----------------- -----------------
---------------- ----------------- -----------------
---------------- ----------------- -----------------

Thread A Thread B Thread C

fig: Multithreaded

Priority in multithreading:-
1. MAX – PRIORITY = 6 – 10
2. NORM – PRIORITY = 5
3. MIN – PRIORITY = 1 – 4

public class cls


{
public static void main(String args[])
{
abc obj1 = new abc( );
xyz obj2 = new xyz( );
obj1.setPriority(Thread.MIN_PRIORITY); / obj1.setPriority(1);
obj2.setPriority(Thread.MAX_PRIORITY); / obj2.setPriority(10);
obj1.start( );
obj2.start( );
}
}

2. example of multithreading by implementing Runnable Interface:-

class cls1 implements Runnable


{
public void run( )
{
int i, j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(“*”);
}
System.out.println( );
}
}
}
class cls2 implements Runnable
{
public void run( )
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=I;j++)
{
System.out.print(“$”);
}
System.out.println( );
}
}
}
public class cls
{
public static void main(String args[])
{
cls1 obj1 = new cls1( );
cls2 obj2 = new cls2( );
Thread t1 = new Thread(obj1);
Thread t2 = new Thread(obj2);
t1.start( );
t2.start( );
}
}

difference between Runnable interface and thread class

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
{
------------------
}

2. Syntax of using Runnable interface and thread class

(thread)
class class_name extends Thread
{
public void run( )
{
statements
}
}

(Runnable)
class class_name implements Runnable
{
public void run( )
{
statements
}
}

3. Reuseability:- In “implements Runnable” we are creating a different class for a


specific task. It gives us freedom to reuse the codes whenever require.
If we “extends Thread” then we cannot reuse the code as it does not support
the concept of multiple inheritance.

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”);
}
}

You might also like