views:

150

answers:

5

Hi,

sleep() is a static method of class Thread. How does it work when called from multiple threads. and how does it figure out the current thread of execution. ?

or may be a more generic Question would be How are static methods called from different threads ? Won't there be any concurrency problems ?

+3  A: 

The sleep method sleeps the current thread so if you are calling it from multiple threads it will sleep each of those threads. Also there's the currentThread static method which allows you to get the current executing thread.

Darin Dimitrov
A: 

Thread.sleep(long) is implemented natively in the java.lang.Thread class. Here's a part of its API doc:

 Causes the currently executing thread to sleep (temporarily cease 
 execution) for the specified number of milliseconds, subject to 
 the precision and accuracy of system timers and schedulers. The thread 
 does not lose ownership of any monitors.

The sleep method sleeps the thread that called it.(Based on EJP's comments) determines the currently executing thread (which called it and cause it to sleep). Java methods can determine which thread is executing it by calling Thread.currentThread()

Methods (static or non static) can be called from any number of threads simultaneously. Threre will not be any concurrency problems as long as your methods are thread safe. You will have problems only when multiple Threads are modifying internal state of class or instance without proper synchronization.

naikus
@naikus Implentation of Thread class says sleep is native method. Any idea how does native code determine what thread to sleep ?
YoK
@Naikus . What i wanted to know is, how are code in static methods run in thread scope . Will there be a copy of each method loaded on to thread's stack ?
JWhiz
@YoK, I am not sure but maybe it calls another native method Thread.currentThread() ;). Im sure you know about it.
naikus
@JWhiz no, there won't be any copy of the method, instaead for java methods, the stack has a "stack frame" for each method invocation that stores the state of the method invocation. But this is only for java methods, not native methods.
naikus
'The sleep method determines the currently executing thread (which called it and cause it to sleep)'. That's not correct. There is no need for Thread.sleep() to do anything except call the operating system's sleep() system call. sleep() is a system call and always causes the current thread to sleep.
EJP
@EJP can you point a resource on where I can verify this?
naikus
@EJP what about the VM's where a java thread may not be mapped to an OS's native thread?
naikus
Then it would still only have to sleep the current thread of execution, which *already* is the current thread. It doesn't have to look itself up. It could just call Object.wait() with a timeout for example.As to resources, it is you that needs to provide a resource to support your claim.
EJP
@EJP, I'd say you are only partially correct, I just verified in openjdk source. The sleep method takes in a java thread argument and calls different methods poll, park or sleep in different OSs. In windows it calls sleep but only if its an uninterruptable thread.
naikus
@JWhiz There won't be a copy of each method loaded on the stack of the current thread, the method is just code - it can be shared between all the threads. What's on the stack of each indiviual thread is all the local variables, stack pointer,etc. of that thread.
nos
@naikus: how exactly does that support your statement?
EJP
@EJP No it doesn't, thats why i've made the changes and credited you in that. Check my answer
naikus
@EJP and oh, it could not "just call" Object.wait() with a timeout because then, it will have to give up the monitors. Check the javadoc. It says that the thread does not lose ownership of the monitors.
naikus
I very carefully didn't say it could call this.wait(). It could create a new object, sync on it, and wait.
EJP
A: 

a more generic Question would be How are static methods called from different threads ? Won't there be any concurrency problems ?

There is only a potential concurrency problem if one or more thread modifies shared state while another thread uses the same state. There is no shared state for the sleep() method.

Peter Lawrey
+1  A: 

how does it figure out the current thread of execution?

It doesn't have to. It just calls the operating system, which always sleeps the thread that called it.

EJP
+1: I couldn't find the correct words.
Martijn Courteaux
A: 

When the virtual machine encounters a sleep(long)-statement, it will interrupt the Thread currently running. "The current Thread" on that moment is always the thread that called Thread.sleep(). Then it says:

Hey! Nothing to do in this thread (Because I have to wait). I'm going to continue an other Thread.

Changing thread is called "to yield". (Note: you can yield by yourself by calling Thread.yield();)

So, it doesn't have to figure out what the current Thread is. It is always the Thread that called sleep(). Note: You can get the current thread by calling Thread.currentThread();

A short example:

// here it is 0 millis
blahblah(); // do some stuff
// here it is 2 millis
new Thread(new MyRunnable()).start(); // We start an other thread
// here it is 2 millis
Thread.sleep(1000);
// here it is 1002 millis

MyRunnable its run() method:

// here it is 2 millis; because we got started at 2 millis
blahblah2(); // Do some other stuff
// here it is 25 millis;
Thread.sleep(300); // after calling this line the two threads are sleeping...
// here it is 325 millis;
... // some stuff
// here it is 328 millis;
return; // we are done;
Martijn Courteaux
There is no 'interrupt'.
EJP