我对静态方法有些困惑。静态方法无权访问 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/