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.

Friday, August 3, 2007

A mystery man came to play in kerala politics

Last week kairali channel went for Farris Aboobecker who donated 60 lakh Rs for E.K Nayanar memorial foot ball play and who gave 1 crore Rs for P.T Usha 's sports school. Everybody thought that he might be an old person.But when I saw him in front of John Brittas (Kairali channel MD) , I surprised, I understood Farris is a young , handsome man. In this story , interesting thing is , Kairali channel is CPI(M) party channel.Why did this party channel take interest in this mystery man??......He was sending arrows against CM of Kerala in his first and second interview. At the same time he refused or intentianally go backward when Brittas asking him about his background and asset . But at present present one this is clear, he is main share holder of Deepika news paper.....This mystery man proved that he could play lot off politrics in kerala.....

Tuesday, July 31, 2007

POLITRICS IN KERALA

I use to see the program POLITRICS in India Vision channel . You guys , don’t need any comedy shows or scenes to laugh, you just need to watch the actions of the MPs and MLAs ( especially ministers)…What a standard people !!!!!!! beauty of words from their mouth is really funny. At present the CM of kerala- achu - is trying to make a revolution in Munnar(may be for improving his image). It was a good movement in my view. All encroachment into the govt. land by the BIG Bs must be prohibited, without harming the poor. He had to face allegation from his own allies. Meanwhile there is a statement from opposition leader- ‘kunjukunju’- that he is going to donate his land to panchayath. It is described as a drama in politrics……It was really a funny drama. Now the GODS OWN COUNTRY is named as “KODI KOZHA COUNTRY” ( crore bribe ): .Deshabimani had to face a crore bond (cartoonist convert this word to bonda) allegation. Party punished the dealers….. But kerala police failed to investigate . For a counter attack , an allegation against congress leader that he had received money (crore) from himalaya group- reinvestigation of kanichikulagara murder case. But one thing must be in all keralites mind that one family is still suffering in that incident–they lost their daughter, son and father. If there were any mistake in probe, govt should go for reinvestigation . But don’t use this for counter attack . Sad thing (in politrics view :funny thing) is that, this case refreshed not as murder case, but as a bribe case.