Concept of Thread refer to running many processes(subprograms) concurrently
within a Main program.
If we consider the Main program as a JOB,
the job can be divided into different tasks and
the tasks which are independent can be processed concurrently
using threads within that Main program.
As simple illustration.
If COOKING a dish is the Main program.
Within it, Heating Oil and Cutting Vegetables are independent tasks.
There is no need to wait to cut vegetables till Oil is heated.
Theses two tasks can be performed concurrently by assigning them two people.
Same thing can be done in java by assigning the tasks to two threads.
We will use the above example and write a Java program with
COOKING as a the Main program and
HEATING OIL and CUTTING VEGETABLE as two threads running concurrently
within the Main program.
In Java, Thread can be created in two ways.
1. Using the RUNNABLE interface
2. By extending THREAD class.
This programs uses RUNNABLE interface.
Runnable interface consists of one method run(), which is executed when
the thread is activated by start() method.
run() method contain the program content of the thread.
Thread automatically ends when run() is completed.
Now,
We create two Runnables, one for the task of Heating Oil and another
for Cutting Vegetables.
The content for each thread (runnables) must be coded in the run() method of
respective runnable class.
After creating the Runnable classes.
In the Main program, Objects are created from the classes.
And, threads are constructed using the objects.
Exception for the threads are caught using InterruptedException, which is
extension of Exception class.
/* HeatingOil Runnable */
class HeatingOil implements Runnable{
public static int HOcount; // counter for iteration
public HeatingOil(){
}
public void run() {
while(HOcount <= 5){
try{
System.out.println("Mts "+(++HOcount) +"- Heating Oil");
Thread.sleep(1000);
} catch (InterruptedException ie) {
System.out.println("Exception in thread: "+ie.getMessage());
}
}
}
}
/* CuttingVegetable Runnable */
class CuttingVegetable implements Runnable{
public static int CVcount;
public CuttingVegetable(){
}
public void run() {
while(CuttingVegetable.CVcount <= 5){
try{
System.out.println("Mts "+ (++CVcount) + "- Cutting Vegetables " );
Thread.sleep(1000); // suspending thread
} catch (InterruptedException ie) {
System.out.println("Exception in thread: "+ie.getMessage());
}
}
}
}
/* In the Main program.
Using the above Runnables corresponding objects are created (HeatingOil,CuttingVegetable).
And for each runnable objects, a thread is constructed ( hot, cvt).
Threads have many methods, methods used in this program are
start() - to start the Thread and it also implicitly executes the run() method.
sleep(n) - to suspend thread for said time
isAlive(t)- to check whether the thread is completed the run() method.
A thread end automatically after completing the run() method.
*/
public class Cooking {
public static void main(String[] args) {
System.out.println("Main Thread...");
HeatingOil ho = new HeatingOil(); // object creation
CuttingVegetable cv = new CuttingVegetable();
Thread hot = new Thread(ho); //thread construction
Thread cvt = new Thread(cv);
hot.start(); // Starting the threads and it will execute the run()
cvt.start(); // implicitely
// loops till both the threads end
while (cvt.isAlive() && hot.isAlive()){
try{
Thread.sleep(1000);
} catch (InterruptedException ie){
System.out.println("Exception in main thread: "+ie.getMessage());
}
}
System.out.println("CUTTING VEG. IS OVER");
System.out.println("HEATING OIL IS OVER");
System.out.println("Main Program COOKING proceeds...");
}
}
Output :
run:
Main Thread...
Mts 1- Heating Oil
Mts 1- Cutting Vegetables
Mts 2- Heating Oil
Mts 2- Cutting Vegetables
Mts 3- Heating Oil
Mts 3- Cutting Vegetables
Mts 4- Cutting Vegetables
Mts 4- Heating Oil
Mts 5- Heating Oil
Mts 5- Cutting Vegetables
Mts 6- Heating Oil
Mts 6- Cutting Vegetables
CUTTING VEG. IS OVER
HEATING OIL IS OVER
Main Program COOKING proceeds...
Click to see Thread creation by extendeding Thread class
No comments:
Post a Comment