Tuesday, August 7, 2007

Multithreaded programming in JAVA

Last days I spend a few time for understanding multithreaded programming in JAVA. In C language it is done by including the header file pthread.h and the function pthread_create use for creating a new thread.

All modern operating systems support multitasking. However there are two distinct type of multitasking: process based and thread based. In thread based multitasking environment, the thread is the smallest unit of dispatch able code. This means a single program can perform two or more tasks simultaneously.

Creating a thread in JAVA:
Java defines two ways in which this can be accomplished.

1. implement the Runnable interface
2. extend the Thread class itself

Implementing Runnable
The easiest way to create a thread is to create a class that implements the Runnable interface. Runnable abstracts a unit of executable code. To implement Runnable, a class need only implement a single method called run().

After implements Runnable, instantiate an object of type Thread from within that class. Thread defines several constructors. The that we will use

Thread (Runnable threadOb, String ThreadName)

After the new thread is created, it will not start running until we call its start()

class NewThread implements Runnable {
Thread t;

NewThread() {
t = new thread(this,”demo thread”);
System.out.println(“child thread: “ + t);
t.start();
}


public void run() {
try {
for(int i=5; i >0; i - -){
System.out.println(“child thread” + i);
Thread.sleep(500);
}
}catch(InterruptedException e){
System.out.println(“Child interrupted “);
}
System.out.println(“child exiting normally”);
}
}

class ThreadDemo {
public static void main(String args[]){
new NewThread();
try{
for(int i=5; i >0; i - -){
System.out.println(“Main thread”);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println("Main thread interrupted");
}
System.out.println("main thread Exiting");
}
}

In this program main thread and child thread will count five to one downwords simultaneously. But execution of main thread will be completed only after completing child thread execution.Because of the thread delay given in for-loops. Main Thread finishes last, because the main thread sleeps for 1000ms, but child thread only for 500ms. In fact some older JVMs, if the main thread finishes before child thread completed, then the java run time system may “ hang”. In above program it is done by sleep. However it is hardly a satisfactory solution.
There are two ways to determine , whether a thread finished . First, call isAlive() on the thread. The isAlive() method returns true if the thre upon which it is called is still running. It returns false otherwise.
class NewThread implements Runnable {
Thread t;
String name;

NewThread(String threadName){
name = threadName;
t = new Thread(this, name);
System.out.println("newthread:" + t);
t.start();
}

public void run(){
try{
for(int i=5; i > 0; i--){
System.out.println(name +":" + i);
Thread.sleep(1000);
}
}catch(InterruptedException e){
System.out.println(name + "interrupted");
}
System.out.println(name +"exiting normally");
}

}
class Demojoin{
public static void main(String args[]){
NewThread ob1 = new NewThread("One");
NewThread ob2 = new NewThread("two");

System.out.println("thread one is alive" + ob1.t.isAlive());
System.out.println("thread two is alive" + ob2.t.isAlive());

try{
System.out.println("waiting for thread to finish");
ob1.t.join();
ob2.t.join();

}catch(InterruptedException e){
System.out.println("main thread interrupted");
}

System.out.println("thread one is alive" + ob1.t.isAlive());
System.out.println("thread two is alive" + ob2.t.isAlive());

System.out.println("main thread exiting");
}
}

In output of this program ob1.t.isAlive and ob2.t.isAlive are true at first call, since threads are running at that time. ob1.t.join and ob2.t.join will wait for the child thread to complete execution.Second call of ob1.t.isAlive and ob2.t.isAlive returns false,since child threads completed their execution.

No comments: