• 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

Static concept in Threads

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4179
22
IntelliJ IDE Python Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic