Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Static concept in Threads  RSS feed

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everybody,
I have been studying threads but couldn't understand the followings :

Q1. Why methods like wait, notify, notifyall are of Object class but not of Thread class ?
Q2. Why sleep() is static ?

Thanks!!!
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Tarun Oohri wrote:Hello everybody,
I have been studying threads but couldn't understand the followings :

Q1. Why methods like wait, notify, notifyall are of Object class but not of Thread class ?


This gets asked a lot. Look at this FAQ: https://www.coderanch.com/how-to/java/WaitAndNotifyInObjectClass


Q2. Why sleep() is static ?


This helps when you properly separate the task to be run from the Thread instance it is run in. Most code that you execute will not be written in a class that extends Thread, so it won't have direct access to the Thread object in which it runs. That means if sleep() were non-static the code which wanted to execute sleep() would have to get a reference to the Thread in which it is running. It could do so by calling the static Thread#currentThread() method. So the code would look like: Thread.currentThread().sleep();. Since this would be the 'normal' case, why make the user always call currentThread()? Especially considering it would give the false impression that you could call sleep() on an arbitrary Thread object (which you can't)? So, to prevent the extra method call, and to make it clear that you can't call sleep() on any Thread other than the current one (by not providing an API that allows it) Thread#sleep() is static.
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Q1. Why methods like wait, notify, notifyall are of Object class but not of Thread class ?



This question has been answered multiple times at CodeRanch. You'd perhaps like to try the search feature? Let us know if you still can't find the answer.

Q2. Why sleep() is static ?


sleep() is always invoked on the current thread, i.e the thread that issued the Thread.sleep() in the first place. Since the job of Thread.sleep() is to put the current thread to sleep, it would have been pointless to have Thread.sleep as an instance method. Hence it is a class method.


 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
God knows why it didn't prompt this time while posting that Steve had already responded to this question and had provided a far better response than mine. :-)

Or may be it did; I didn't look well.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!