Monday, 13 May 2013

Thread class - JAVA


Thread class is present in java.lang package. This class implements Runnable interface. This class allows user to create multiple threads in application. These threads can execute code in concurrent way. Every thread 
is having some priority between 1 to 10. By default it set to 5. Every thead can create another thread. The priority of newly created thread is same as that of parent thread, but you can change this mannually. Thread 
can be daemon thread so new thread created by daemon will be daemon thread. 
We can create thread by extendding class Thread or by implementing Runnable interface. Best way is to implement Runnable interface because it will allow your class to extend another class so it will be flexible 
architecture.
Here is example by extending thread class,

class ThreadClass extends Thread {
public void run() {
try {
System.out.println("In run : 1");
Thread.sleep(2000);
System.out.println("In run : 2");
} catch(Exception e) { 
System.out.print("Exception : " + e);
}
}
public static void main(String args[]) {
ThreadClass tc = new ThreadClass();
tc.start();
}
}


And here is example by implementing runnable interface,

class ThreadClass implements Runnable {
private Thread th;
public ThreadClass() {
th= new Thread(this);
th.start();
}
public void run() {
try {
System.out.println("In run : 1");
Thread.sleep(2000);
System.out.println("In run : 2");
} catch(Exception e) { 
System.out.print("Exception : " + e);
}
}
public static void main(String args[]) {
ThreadClass tc = new ThreadClass();
}
}


Here you can see that class extending Thread class is not having any chance to extends any class but class implementing Runnable is having that chance.

Thread class is having many constructors like given below,
Thread();
Thread(String name);
Thread(ThreadGroup group, String name);
Thread(Runnable target, String name);
Thread(ThreadGroup group, Runnable target, String name);
Thread(ThreadGroup group, Runnable target, String name, long stackSize);
Every constructor internally calls init method of Thread class which is described below.

Thread class gives start() method which is synchronised by nature and you can't restart any thread which has completed its execution or you can't call start() method more than onse on same thread otherwise it will throw
IllegalThreadStateException. This start method calls another start0() native method. 

stop() method stops the execution of thread. In this method SecurityManager comes into picture. For stoping thread help of ThreadDeath class which is subclass of Error class is taken.

Thread.currentThread() method returns reference to current thread. Onse you are getting reference to current thread you can perform different operations on it.

Thread.sleep(timeInMilliSec) This method makes thread to sleep for given time.

Thread.sleep(milliSec, nanoSec) This makes thread to sleep for given milli seconds and nano seconds.

Thread.init(group, target, name, stackSize); This method initializes thread where group is the thread group, target is the target object whose run method we have to execute, name of thread an stack size for thread.
Other properties like priority, daemon or not will be copied from parent thread or you can set then mannually.

Thread.yield() method makes current thread to pause temporarly and allows other threads to run.

interrupt() method interrupts thread. This methos calls interrupt0() native method internally.

isInterrupted() method tells you whether given thread is interrupted or not.

isAlive() method returns boolean value stating whether thread is alive or not.

setPriority() method sets priority of thread. This priority can be between 1 to 10 where 1 is lower priority and 10 is higher priority.

activeCount() returns number of active threads in current thread group.

join(), join(milliSec), join(milliSec, nanoSec) waits for thread to die.

setDaemon(boolean), isDaemon() are methods related with daemon threads.

Along with these methods Thread class gives other methods like suspend(), resume() etc.

No comments:

Post a Comment