简体   繁体   中英

How does Thread.sleep() method work?

I saw similar questions here but I think mine is not a duplicate. I cannot understand this example:

public static void main(String[] args) throws InterruptedException {
    Thread t1 = new Thread() {
        public void run() {
            System.out.println("t1");
        }
    };

    Thread t2 = new Thread() {
        public void run() {
            System.out.println("t2");
        }
    };


    t1.start();
    Thread.sleep(1000);
    t2.start();
    Thread.sleep(1000);

    System.out.println("main");
}

Why does it print thread names always in the same order?

t1
//1 sec pause
t2
//1 sec pause
main 

Why does Thread.sleep(1000) after t1.start() force t2 thread to wait? Why does t2 not start execution of run method immediately?

Think of your program as a piece of string (Thread), when you run your program (Thread.start) you start to place beads on that piece of string (your code). This means that one must go on after the other, if you sleep (Thread.sleep) its like your putting down that piece of thread for the time that you specify, so your no longer placing beads on it (simple analogy for those that don't understand).

What you've done is gotten your first thread (main) then started 2 new threads (t1, t2) but they are not functional as of yet, when you call Thread.start() they become functional. Your current functional thread (main) has a sleep command which will pause all activity in that thread for 1 second. So your program is ordered like this: create threads 1 and 2 (t1, t2), start thread 1 (t1) then pause for 1 second, then start thread 2 (t2), then pause for one second, then print the string "main";

因为此代码中的sleep方法具有运行main()的线程,所以在启动另一个线程与启动下一个线程之间要暂停一秒钟。

When your program starts it begins in the "Main" thread,

The call the "Thread.start()" forks the execution ( causing t1 to begin in parallel), then tell the "Main" thread to sleep for 1 second, Then you start the second thread, and then finally you print "Main"

It seems like you're forcing t2 to wait because you haven't started t2 yet when t1 is started.

Consider you plus two assistants, Alice and Bob. You tell the two of them that when you say their name, they should respond by saying their name. Your current execution is:

  1. You say Alice
  2. Wait 1 second
  3. You say Bob
  4. Wait 1 second
  5. You say your own name

Alice replies with her name when you say Alice, which occurs while you are waiting. It seems like Bob is waiting, and in a way he is - he's waiting for you to say his name (start his thread). Of course Alice will always (or almost always) say her name before Bob. It doesn't take a full second for Alice to hear her name and reply in kind. Contrast this with the following execution:

  1. You say Alice
  2. You say Bob
  3. Wait 2 seconds
  4. You say your own name

In this paradigm, you don't actually know which name will be said first. You said alice first, but the two have no space between them so it depends which of Alice and Bob have quicker reflexes. This would correspond to:

t1.start();
t2.start();
Thread.sleep(2000);
System.out.println("main");
  1. The thread2 is not in the running state until its start() is called.
  2. The first Thread.sleep(1000) makes the main thread pause.
  3. The following code modified can make what you expected:

     thread1.start(); thread2.start(); Thread.sleep(2000); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM