I am a bit confused with Thread.sleep()
method. if Thread.sleep()
is a static method, how does two threads know which is put to sleep. For example, in the code below, I have two three Threads
main
, t
and t1
. I call Thread.sleep()
always. Not t.sleep()
. Does it mean Thread.sleep() puts the current Thread to sleep? That means a Thread instance puts to sleep by itself by calling the static method. what if t1 wants to put t
to sleep. that shouldn't be possible correct?
public class ThreadInterrupt {
public static void main(String[] args) throws InterruptedException {
System.out.println("Starting.");
Thread t = new Thread(new Runnable(){
@Override
public void run() {
Random ran = new Random();
for (int i = 0; i < 1E8; i++) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
System.out.println("we have been interrupted");
e.printStackTrace();
}
});
Thread t2 = new Thread(new Runnable() {
public void run() {
//some stuff
}
});
t.start();
t2.start();
Thread.sleep(500);
t.interrupt();
t.join();
System.out.println("Finished.");
}
}
The code would only execute when someXThread was executing, in which case telling someYThread to yield would be pointless. So since the only thread worth calling yield on is the current thread, they make the method static so you won't waste time trying to call yield on some other thread.
The sleep() method is used to stop the execution of the current thread(whichever might be executing in the system) for a specific duration of the time and after that time duration gets over, the thread which is executing earlier starts to execute again.
You can call Thread. getState() on and check if the state is TIMED_WAITING . Note, however that TIMED_WAITING doesn't necessarily mean that the thread called sleep() , it could also be waiting in a Object.
Thread.sleep () is a static method of Thread class, which means we have to call it using Thread class name. It is used to make a thread sleep for some amount of time. There are two variants of it: public static void sleep (long millis) throws InterruptedException public static void sleep (long millis, int nanos) throws InterruptedException
There are two overloaded methods of Sleep () method present in Thread Class, one is with one argument and another one is with two arguments.
To put it plainly, Thread.sleep () makes the WebDriver wait for a specific time before execution and this happens only once. On the other hand, setSpeed () sets the desired speed of execution or delays execution by a specified amount of time before each operation.
Following are the syntax of the sleep () method. The method sleep () with the one parameter is the native method, and the implementation of the native method is accomplished in another programming language. The other methods having the two parameters are not the native method.
Does it mean Thread.sleep() puts the current Thread to sleep?
Yes. Only the current thread can do that.
What if t1 wants to put t to sleep. that shouldn't be possible correct?
Right. You can set a volatile boolean
flag that will cause another thread to call Thread.sleep(...)
but another thread can't cause a thread to sleep.
volatile boolean shouldSleep = false;
...
// check this flag that might be set by another thread to see if I should sleep
if (shouldSleep) {
Thread.sleep(...);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With