MULTITHREADING
Process: A program in execution Is called as a process.
Processes are of two types They are,
[Link]-weight process
[Link]-weight process
Heavy-weight process:
A process which requires more memory and more CPU time is called Heavy-weight process.
Light-weight process:
A process which requires less memory and less CPU time is called Light-weight process.
Thread:
A thread is a light-weight process.
(or)
A thread is the path followed when executing a program.
(or)
A thread is a single sequential flow of control in a program
Lifecycle of a thread:
10 marks
A thread can be in one of the following states
[Link] state
[Link] state
[Link] state
[Link] state
[Link] state
When an object of the thread class is created then it is said to be a New born state.
[Link] state
When the thread is in a queue waiting for the cpu then it is said to be in Runnable state.
[Link] state
When the thread is being executed by the cpu then it is said to be Running state.
4. Blocked state
A thread enters into blocked state if one of the following methods are called
[Link] ()
[Link] ()
[Link] ()
[Link] state
Once the code of the thread is executed then it enters into dead state from the blocked state if a
thread wants to enter into running state then the following methods are called
[Link] ()
[Link] ()
Creation of threads:
A thread can be created using two methods
[Link] extending the thread class
[Link] implementing the Runnable interface
Assining priorities to threads:
Set priority () method is used to assign priority to the threads.
MIN-PRIORITY-1
NORM- PRIORITY-5 constants
MAX- PRIORITY-10
Types of threads:
Threads are classified into two types
[Link] threads
[Link] threads
User threads
The threads which are created by the user (or)programmer are called user threads.
Daemon threads
The threads which are executing in the background process are called Daemon threads.
// Java program for creating multiple threads using thread class.
Class A extends Thread
{
Public void run ()
{
For (int i=1; i<=5; i=i+1)
{
[Link] (“\n\n\t i=” +i);
}
}
}
Class B extends Thread
{
Public void run ()
{
For (int j=1! j<=5; j=j+1)
{
[Link] ("\n\n\t j= "+j);
}
}
}
Class C extends Thread
{
Public void run ()
{
For (int k=1; k=5; k=k+1)
{
[Link] ("\n\n\t k "+k);
}
}
}
Class Demo1
{
Public static void main (String args[])throwsException
{
Aa=new A ();
Bb=new B ();
Cc=new C ();
[Link] ();
[Link] ();
[Link] ();
}
}
// Java program for creating multiple threads using runnable interface
class A implements Runnable
{
public void run ()
{
for (int i=1; i<=s; i="+i);
{
[Link]("\n\n\t i= "+i);
}
}
}
class B implements Runnable
{
Public void run ()
{
For (int j=1; j<=5; j=j+1)
{
[Link]('\n\n\t j="+j);
}
}
}
Class C implements Runnable
{
Public void run ()
{
For (int k=1; k<=5; k=k+1)
{
[Link]("\n\n\t k="+k);
}
}
}
Class Demo2
{
Public static void main (String args[])throwsException
{
A a= new A ();
B b= new B ();
C c= new C ();
Thread t1=new thread (a);
Thread t2=new thread (b);
Thread t3=new thread (c);
[Link] ();
[Link] ();
[Link] ();
}
}
10 marks
//Java program for assigning priorities to threads
Class A extends Thread
{
Public void run ()
{
For (int i=1; i<=s; i=i+1)
{
[Link](\n\n\t i= "+i);
}
}
}
Class B extends Thread
{
Public void run ()
{
For (int j=1; j<=s; j=j+1);
{
[Link] (\n\n\t j= "+j);
}
}
}
Class C extends thread
{
Public void run ()
{
For (int k=1; k<=s; k=k+1)
{
[Link]("\n\n\t k= "+k);
}
}
}
Class Demo3
{
Public static void main (String args[])throwsException
{
A a= new A ();
B b= new B ();
C c= new C ();
[Link] priority (Thread.MAX_PRIORITY);
10 marks
1
5
6
7