Reputation: 1451
sleep function in thread class is static.i read sleep function can make a thread sleep for a particular time while others threads running.
As sleep function is static ...when it is called it will be applicable to all threads.how it will be used to keep particular thread in sleep state.
Upvotes: 0
Views: 773
Reputation: 15656
Since the method depends on the state of the jvm calling thread and not on the thread represented by an object it has to be static, anything else would be misleading.
Implementing it to work on Thread instances would not work out well, since stopping other threads can cause the complete jvm to halt if locks to jvm resources are held (link).
Upvotes: 0
Reputation: 1376
According to the java documentation :
public static void sleep(long millis,
int nanos)
throws InterruptedException
Causes the currently executing thread to sleep (cease execution) for the specified number of milliseconds plus the specified number of nanoseconds. The thread does not lose ownership of any monitors.
So when you call sleep()
, you will sleep the current thread.
Upvotes: 0
Reputation: 240996
Thread.sleep();
will put current thread from which this code is executed in sleep mode
Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds.
Upvotes: 2
Reputation: 22741
The sleep method is not applicable to all threads, when called it obtains the current thread inside it (probably using another static method, Thread.currentThread()). The method call invocation is only applicable to the current thread due to the principles of heap/stack visibility, and doesn't modify any static fields (it is self-contained).
Upvotes: 1