java - Thread 类的静态 sleep 方法如何在不访问 'this' 引用的情况下工作?

标签 java multithreading methods this sleep

我对静态方法有些困惑。静态方法无权访问 this 引用。 (在 Java 中,this 是引用当前对象的引用。)

当调用Thread.sleep(millis)时,Thread类的静态sleep方法是如何选择让哪个线程 hibernate 的? Thread.sleep(long millis) 是静态方法,无法访问 this 引用。

public class CurrentThreadDemo {
    public static void main(String... args) {

        Thread t = Thread.currentThread();

        System.out.println("Current thread: " + t);

        //change the name of the thread
        t.setName("My thread");
        System.out.println("After name change: " + t);

        try {
            for (int n = 5; n > 0; n--) {
                System.out.println(n);
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

最佳答案

When there is a call to Thread.sleep(millis), how does the static sleep method of Thread class choose to which thread to sleep.

Thread.sleep 的规范是它挂起当前 线程,即调用sleep 方法的线程。

关于java - Thread 类的静态 sleep 方法如何在不访问 'this' 引用的情况下工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29575688/

相关文章:

java - 使用匹配的正则表达式调用子类的方法

java - 为什么在 java 中键入带参数的构造函数时不提供默认构造函数?

java - 重复变化的代码(组合)?

multithreading - 从32位XP迁移到64位Win7-线程含义

multithreading - 在 OnExecute 事件中使用数据库 (Indy)

java - 句子生成器java

php - 如果我为每个插入请求调用一个方法,我是不是在浪费服务器时间

Java synchronized() 关键字——防止字段中引用的对象发生变化?

python - 多处理超时?

java - 构造函数不工作,稍后调用时不接受参数?