Thread class 的静态睡眠方法如何在不访问 'this' 引用的情况下工作?
How does static sleep method of Thread class work without access to 'this' reference?
我对静态方法有些困惑。静态方法无法访问 this
引用。 (在Java中,this
是引用当前对象的引用。)
当有调用Thread.sleep(millis)
时,Thread
class的静态sleep方法如何选择哪个线程休眠? 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();
}
}
}
"The Thread.sleep()
method essentially interacts with the thread
scheduler to put the current thread into a wait state for the required
interval."
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
方法的线程。
When there is a call to Thread.sleep(millis), how does the static sleep method of Thread class choose which thread to sleep?
选择当前线程。调用方法的那个。 Thread.currentThread().
返回的那个
Thread.sleep(long millis) is a static method and does not have access to this reference variable.
我同意。所以?它不需要它。
我对静态方法有些困惑。静态方法无法访问 this
引用。 (在Java中,this
是引用当前对象的引用。)
当有调用Thread.sleep(millis)
时,Thread
class的静态sleep方法如何选择哪个线程休眠? 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();
}
}
}
"The
Thread.sleep()
method essentially interacts with the thread scheduler to put the current thread into a wait state for the required interval."
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
方法的线程。
When there is a call to Thread.sleep(millis), how does the static sleep method of Thread class choose which thread to sleep?
选择当前线程。调用方法的那个。 Thread.currentThread().
Thread.sleep(long millis) is a static method and does not have access to this reference variable.
我同意。所以?它不需要它。