• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ..

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks :-)
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic