这个Java代码中的`Thread`是什么?

One*_*ero 2 java concurrency multithreading

public class SleepMessages {
    public static void main(String args[])
        throws InterruptedException {
        String importantInfo[] = {
            "Mares eat oats",
            "Does eat oats",
            "Little lambs eat ivy",
            "A kid will eat ivy too"
        };

        for (int i = 0;
             i < importantInfo.length;
             i++) {
            //Pause for 4 seconds
            Thread.sleep(4000);
            //Print a message
            System.out.println(importantInfo[i]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在这段代码中没有创建线程,但确实存在Thread.sleep(4000).那么这个线程代表什么呢?主程序本身?换句话说,Thread是否隐含了程序本身?

Val*_*ris 5

是的,Thread.sleep()作用于当前线程,在您的情况下,它只是运行程序时必须存在的一个线程.

  • 这通常称为"主"线程. (2认同)