Thursday, January 20, 2011

Java's Exception on Thread

 A thread can be in any one state and when we force our thread to stop temporarily through sleep ( ). Threads are typically spliced into a time determined by the processor. So, base on thread scheduling, treads are executed by spliced parts.

So, when the treads are interrupted, we call InterruptedException to catch any error during inactive state. The sleep method makes of milliseconds to put the thread in inactive mode.

See the program below for your sample.
[+/-] show/hide

class HelloThread2{
public static void main(String[] args){
HelloThread1 myHello=new HelloThread1();

class HelloThread1 extends Thread {
private static final int TIMES =5;
public void run()
for(int x=1;x<=TIMES;x++) { System.out.println("Hello"); if (TIMES==4) try{ sleep(60000); } catch (InterruptedException e) { } } } }