Basically there are two types of multitasking. They are
- Process based multitasking.
- Thread based multitasking.
Process based multitasking is one where two or more programs execute simultaneously on the other hand the thread-based multitasking is one where the same program performs more than one task simultaneously.
Multithreading is nothing but a specialized form of thread based multitasking.
Java is designed for a networked environment. In a networked environment the ideal time of the CPU is the most. To make the maximum usage of the CPU ideal time we can design a multithread program. In Java multithreading referees to the simultaneous execution of two or more blocks of code, with in the same program.
Threading can be performed by using the class thread from the package Java.lang or by implementing an interface called runnable interface.
Thread class: Allows you to create a child thread.
Constructors:
- thread(String thread_name);
- thread(runnable object, String thread_name);
Allows you to create a thread object.
Methods:
- void setName(String thread_name); Allows you to set a name fore the thread.
- String getName(); Returns the name of the thread.
- Static void sleep(unsigned milliseconds);Throws interrupted Exception.Suspends the execution of the thread for the specific time interval.
- Void start();Starts the execution of the ‘thread’.
- Thread currentThread();Returns a reference for the current thread.
- Void stop();Stops a thread.
Runnable interface:
public void run();constitutes the code for the child thread.
A thread exhausts at several States
- A thread can be running.
- A running thread can be suspended.
- A suspended thread can be resumed.
- A thread can be stopped.(once stopped it cannot be resumed)
import java.lang.*;
class App62
{
public static void main(String yck[])
{
try
{
for(int n=5;n>0;n--)
{
System.out.println(n);
Thread.sleep(5000);
}
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
output:
\>javac App62.java
\>java App62
5
4
3
2
1
Creating child threads:
Child threads can be created in two different ways
- By extending the thread class.
- By implementing the running interface.
By implementing the running interface.
Procedure:
- Create a class, which implements the “runnable” interface.
- Create a “thread” object of the class using the second constructor of the “thread” class.
- Call the “start()” of the “thread” class.
- Design the body of the “run()”. The “run” constitutes the code of the child thread. Infect start gives a call to the “run”.
class ChildThread implements Runnable
{
Thread t;
ChildThread()
{
t=new Thread(this,"Child Thread");
t.start();
}
public void run()
{
try
{
for(int k=5;k>0;k--)
{
System.out.println(t.getName()+"->"+k);
t.sleep(2500);
}
System.out.println(t.getName()+" exits...");
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
class App63
{
public static void main(String yck[])
{
try
{
ChildThread CT=new ChildThread();
for(int n=5;n>0;n--)
{
System.out.println("Main ->"+n);
Thread.sleep(5000);
}
System.out.println("Main thread exits ...");
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
output:
\>javac App63.java
\>java App63
Main ->5
Child Thread->5
Child Thread->4
Main ->4
Child Thread->3
Child Thread->2
Main ->3
Child Thread->1
Child Thread exits...
Main ->2
Main ->1
Main thread exits ...
Note: The output may differ for each time of execution this will be performed by speed and periority of your system.
By extending the thread class.
Procedure:
- create a class, which extends the thread class. The “thread” class by default implements the runnable interface.
- Specify the name for the thread.
- Call the “start()” of the thread class.
- Design the body of the “run()”. The “run()” constitutes the code of the child thread. Infact “start()” gives a call to “run”.
class ChildThread extends Thread
{
ChildThread()
{
super("Child Thread");
start();
}
public void run()
{
try
{
for(int k=5;k>0;k--)
{
System.out.println(getName()+"->"+k);
sleep(2500);
}
System.out.println(getName()+" exits...");
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
class App64
{
public static void main(String yck[])
{
try
{
ChildThread CT=new ChildThread();
for(int n=5;n>0;n--)
{
System.out.println("Main ->"+n);
Thread.sleep(5000);
}
System.out.println("Main thread exits ...");
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
output:
\>javac App64.java
\>java App64
Main ->5
Child Thread->5
Child Thread->4
Main ->4
Child Thread->3
Child Thread->2
Main ->3
Child Thread->1
Child Thread exits...
Main ->2
Main ->1
Main thread exits ...
Note: The output may differ for each time of execution this will be performed by speed and periority of your system.
class ChildThread extends Thread
{
ChildThread(String tname)
{
super(tname);
start();
}
public void run()
{
try
{
for(int k=5;k>0;k--)
{
System.out.println(getName()+"->"+k);
sleep(2500);
}
System.out.println(getName()+" exits...");
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
class App65
{
public static void main(String yck[])
{
try
{
ChildThread CT=new ChildThread("First");
ChildThread CT1=new ChildThread("Second");
ChildThread CT2=new ChildThread("Third");
for(int n=5;n>0;n--)
{
System.out.println("Main ->"+n);
Thread.sleep(5000);
}
System.out.println("Main thread exits ...");
}
catch(InterruptedException e)
{
System.out.println(e);
}
}
}
output:
\>javac App65.java
\>java App65
Main ->5
Second->5
Third->5
First->5
Second->4
Third->4
First->4
Main ->4
First->3
Third->3
Second->3
First->2
Third->2
Second->2
Main ->3
First->1
Third->1
Second->1
First exits...
Third exits...
Second exits...
Main ->2
Main ->1
Main thread exits ...
Note: The output may differ for each time of execution this will be performed by speed and periority of your system.
