aspose file tools*
The moose likes Threads and Synchronization and the fly likes Static concept in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Static concept in Threads" Watch "Static concept in Threads" New topic
Author

Static concept in Threads

Tarun Oohri
Ranch Hand

Joined: Feb 20, 2013
Posts: 170
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!!!
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3971
    
  17

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.


Steve
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 839
    
  14
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
Bartender

Joined: Sep 06, 2012
Posts: 839
    
  14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static concept in Threads
 
Similar Threads
why sleep is static in thread
outer class
doubt in sleep() method
Concurrency problem
static class