我对于静态方法有些困惑。静态方法没有访问“this”引用的能力。(在Java中,“this”是一个指向当前对象的引用。)当调用“Thread.sleep(millis)”时,线程如何选择静态的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();
}
}
}