A process is an application running your system, where as the thread is an integral part of the process.
That means, A process can contain multiple threads.
Ex:
A browser can handle multiple parallel websites at a time
A movie player can handle audio, video and subtitles at a time.
Let us see the program for Multi threading in java.
import java.lang.*;
class FirstThread extends Thread
{
public void run()
{
for(int i=0; i<4; i++)
{
try
{
if(i == 3)
{
sleep(4000);
}
}
catch(Exception x)
{ }
System.out.println(i);
}
System.out.println(" First Thread Finished ");
}
}
class SecondThread extends Thread
{
public void run()
{
for(int i=0; i<4; i++)
{
System.out.println(i);
}
System.out.println(" Second Thread Finished ");
}
}
class ThirdThread extends Thread
{
public void run()
{
for(int i=0; i<4; i++)
{
System.out.println(i);
}
System.out.println(" Third Thread Finished ");
}
}
class MultiThread
{
public static void main(String arg[])
{
FirstThread a1 = new FirstThread();
SecondThread b1 = new SecondThread();
ThirdThread c1 = new ThirdThread();
a1.start();
b1.start();
c1.start();
}
}
OUTPUT:
0 0 1 2 3 1 2 0 1 2 3 Second Thread Finished Third Thread Finished 3 First Thread Finished
Check an another example for multi threading