Results 1 to 8 of 8
- 05-11-2011, 04:00 AM #1
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
Why Some Thread.sleep() Are Not Interrupted?
First, see this code.
Very simple, there are 1000 threads, named thread1~thread500 and thread1-sleep~thread500-sleep.
Every threadi-sleep sleeps for 10 seconds and threadi interrupts it:
Java Code:public class InterruptTest { public static void main(String[] args) throws Exception { for (int i = 1; i <= 500; i++) { Thread t = new Interrupt(); t.setName("thread" + i); t.start(); } } private static class Interrupt extends Thread { public void run() { Thread t = new Sleep(); t.setName( getName() + "-sleep" ); t.start(); t.interrupt(); System.out.println( System.currentTimeMillis() + " " + t.getName() + " " + t.isInterrupted() ); } } private static class Sleep extends Thread { public void run() { try { Thread.sleep(10000); } catch (Exception e) { return; } System.err.println( System.currentTimeMillis() + " No! " + getName() + " " + isInterrupted() ); } } }
Java Code:1304393164531 No! thread349-sleep true 1304393164562 No! thread428-sleep false
As you can see, I let it output a line after every interrupt() statement. And I found 2 lines of output in stdout, I put them together with the stderr output:
Java Code:1304393163531 thread349-sleep true 1304393163562 thread428-sleep true 1304393173531 No! thread349-sleep true 1304393173562 No! thread428-sleep false
P.S. I have tried 3 computers, they are all Windows XP. And this situation happened on 2 of them, which are Core 2 Duo 2.4GHz. And it didn't happend on another PC, which is Celeron 1.5GHz! Their JDK are the same, version 1.6.
- 05-12-2011, 04:13 AM #2
It's entirely possible that an Interrupt thread could be suspended after calling t.start() and not call t.interrupt() until after t has finished executing. Or a Sleep thread could be interrupted after sleeping, but before printing its message.
- 05-12-2011, 04:44 AM #3
Remember you have 1000 threads. That is a crapload of code executing at the same time. You can't really predict what may or may not happen under such heavy load! However, that is quite weird how the same object prints "true" for "isInterrupted()" and 10 seconds later, it prints false O.o?
- 05-12-2011, 10:17 AM #4
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
- 05-12-2011, 10:20 AM #5
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
- 05-12-2011, 10:22 AM #6
Member
- Join Date
- May 2011
- Posts
- 4
- Rep Power
- 0
Would any of you run this code on your own machine and see if the problem also happens? (You need to run many times to see the stderr output)
- 05-12-2011, 10:45 AM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 05-12-2011, 09:44 PM #8
I don't know enough about the internals of the Thead class to say for sure what might be happening. It might even be JVM-specific. But it's no surprise that you're seeing it on your multi-core CPU and not on the others.
The JVM specification allows threads to cache their own copies of variables, and the caches aren't synchronized (in the general sense) unless you synchronize all access to shared variables (in the Java keyword 'synchronized' sense). So it may be a synchronization issue, not just a timeslicing issue.
Similar Threads
-
Thread Doesn't Sleep
By Creativelymad in forum Threads and SynchronizationReplies: 3Last Post: 03-07-2011, 05:29 AM -
Thread.sleep
By Gog in forum New To JavaReplies: 3Last Post: 01-13-2011, 09:14 AM -
Difference between Thread.yield() and Thread.sleep() methods
By Nageswara Rao Mothukuri in forum New To JavaReplies: 12Last Post: 07-30-2010, 06:37 PM -
how to reduce the thread sleep time and wake up the thread
By baktha.thalapathy in forum Threads and SynchronizationReplies: 2Last Post: 06-24-2010, 08:36 PM -
How to use the sleep and thread?
By jiuhu in forum Java AppletsReplies: 4Last Post: 08-07-2007, 03:56 AM
Bookmarks