Reputation: 32331
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 ??Upvotes: 1
Views: 75
Reputation: 4189
1.The Thread is not comng out of sleep state ?
He actually is but
System.out.println("Out of sleep");
is never executed because when you interrupt Thread.sleep(10000); throws a exception and
e.printStackTrace();
is execute instead
Upvotes: 1
Reputation: 1502086
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...
Upvotes: 4