Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread.sleep() is not sleeping !!! Isn't it ?  RSS feed

 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Everyone

Given the fact that Thread.sleep() thows Interrupted Exceptions . It means that , this thread checks the state continuously for being interrupted or not, which essentially means
that this thread though sleeping from the Runnable point of view is not actually sleeping but still being scheduled for checking the state .

Please clear me this concept ..

 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are looking at it from the wrong end
The JVM (which effectively controls the thread pool) monitors the thread states, not the thread which is being put to sleep. When you invoke Thread#sleep it will set it's state to TIMED_WAITING and relinquish control.
 
Henry Wong
author
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sangel Kapoor wrote:
Given the fact that Thread.sleep() thows Interrupted Exceptions . It means that , this thread checks the state continuously for being interrupted or not, which essentially means
that this thread though sleeping from the Runnable point of view is not actually sleeping but still being scheduled for checking the state .


Given this argument, you can also argue that threads are never blocked waiting for I/O either -- meaning the "thread checks the state continuously for being [I/O exception] or not"...

Of course, the thread is not polling continuously. That would make the sleep() method call very inefficient.... Basically, the thread is asleep (not running), and the act of interruption makes it runnable again (wakes it up). Later when the thread runs (wakes up), it will check why it is up, and if it is due to interruption, it will throw the exception.

Henry
 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THanks and Bunch !!! , Maneesh and Henry Sir :-)

I would like to ask , how can one build such knowledge about multithreading, is there any book, documentation that explains all these concepts.

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, if you're Henry, you can actually *write* the book about that :-) But seriously, Oaks/Wong and Goetz et al. are the two eminent books on Java concurrency.
 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks :-)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!