I have this program below
package com;
public class ThreadDemo implements Runnable {
@Override
public void run() {
while(true)
{
try {
System.out.println("Into sleep");
Thread.sleep(1000000000);
System.out.println("Out of sleep");
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void main(String args[]) {
ThreadDemo tD = new ThreadDemo();
Thread t1 = new Thread(tD);
t1.start();
t1.interrupt();
}
}
I have started the Thread , and will call its run method and goes into sleep state for the above specified seconds .
i have called t1.interrupt();
This is the screen shot
My question is that why
1.The Thread is not coming out of sleep state ?
Into sleep
is printed twice ??
You're in a loop:
So it is "coming out of sleep state" (otherwise you wouldn't see the stack trace) - but you're then calling Thread.sleep
again.
If you only want to sleep once, get rid of the loop...
See more on this question at Stackoverflow