8

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.");
    }

}
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
brain storm
  • 30,124
  • 69
  • 225
  • 393

2 Answers2

9

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(...);
 }
Gray
  • 115,027
  • 24
  • 293
  • 354
  • I think so @SotiriosDelimanolis. Depends on the situation. I've got a lot of my own timer loops that need it of course. Sometimes I'm polling some service that needs to be in a sleep loop. Sometimes I way to be extra responsive without spinning so I add a little sleep. Depends on the situation. – Gray Feb 28 '14 at 19:42
  • how will the boolean flag work, do you mean `t` sets the boolean flag of `t1`? but each thread will have its own scope correct? – brain storm Feb 28 '14 at 19:43
  • Right @user1988876. The `shouldSleep` should be within `t`'s class or something. – Gray Feb 28 '14 at 19:44
  • each thread gets its own stack; how will the boolean flag of t1 available outside that stack to thread t? I mean is it really possible to set a boolean flag of t1 from t? – brain storm Feb 28 '14 at 19:52
  • It is @user1988876. You would say `t1.shouldSleep = true;`. It is `volatile` so the update will be seen by `t1`. See this tutorial: http://docs.oracle.com/javase/tutorial/essential/concurrency/sync.html – Gray Feb 28 '14 at 19:54
  • so shouldSleep must be a Boolean Object that is stored on heap? – brain storm Feb 28 '14 at 19:57
  • `shouldSleep` should be a `volatile` field (`boolean` is fine) and should be a field on the `t1` `Runnable` object, yes. – Gray Feb 28 '14 at 19:58
  • @Gray: I have tried something similar to what you suggested in comments for changing boolean flags in threads. I have an issue which I have posted here: http://stackoverflow.com/questions/22180458/how-to-set-boolean-flag-of-thread-1-from-thread-2-in-java-multithreading can you kindly look at it when you find time.Thanks – brain storm Mar 04 '14 at 19:06
0

It can find the current thread from Thread.currentThread(). The current thread only can put itself to sleep.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186