• Post Reply Bookmark Topic Watch Topic
  • New Topic

What does it mean for sleep(millisecs) to throw an InterruptedException?

 
Mustafa Garhi
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A thread could be interrupted even when it was not sleeping.

Why does the sleep() have to specifically handle this?

Why is run() itself not the right method to throw that exception asking us to declare/handle it?
 
Rob Spoor
Sheriff
Posts: 20816
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to Threads and Synchronization.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
run() can't throw it since then you couldn't catch it -- you don't call run(), the Thread class does.

Sleep() does this because calling interrupt() is a convenient way to tell a thread to shut down; throwing the exception gives your run() method a way to clean up before exiting.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!