我只是从我的课本中研究一些例子,在 Eclipse 中编写代码来解决它。
这是我有疑问的方法的代码:
public void run() {
//Get the lock before entering the loop
synchronized(getClass()) {
for (int i =0; i<N; i++) {
System.out.println(getName() + " is tired");
try{
Thread.currentThread().sleep(DELAY);
} catch (InterruptedException e) {}
System.out.println(getName() + " is rested");
}
在线与:
Thread.currentThread().sleep(DELAY)
Eclipse 发出警告:“应以静态方式访问类型 Thread 的静态方法 sleep(long)”。Eclipse 建议以下解决方案:
Thread.currentThread();
Thread.sleep(DELAY);
我不明白为什么它会有所作为(如果确实如此的话)。有人可以解释一下吗?