wait() 및 notify()는 정적이 아니므로 컴파일러는 정적 컨텍스트에서 wait를 호출해야한다는 오류를 제공해야합니다.wait 및 notify는 정적이 아닙니다.
public class Rolls {
public static void main(String args[]) {
synchronized(args) {
try {
wait();
} catch(InterruptedException e)
{ System.out.println(e); }
}
}
}
다음 코드는 올바르게 컴파일되어 실행됩니다. 왜 컴파일러는 여기에 오류를주지 않습니까? 또는 컴파일러가 이전 코드에서 정적 컨텍스트에서 wait를 호출해야한다는 오류를주는 이유는 무엇입니까?
public class World implements Runnable {
public synchronized void run() {
if(Thread.currentThread().getName().equals("F")) {
try {
System.out.println("waiting");
wait();
System.out.println("done");
} catch(InterruptedException e)
{ System.out.println(e); }
}
else {
System.out.println("other");
notify();
System.out.println("notified");
}
}
public static void main(String []args){
System.out.println("Hello World");
World w = new World();
Thread t1 = new Thread(w, "F");
Thread t2 = new Thread(w);
t1.start();
t2.start();
}
}
당신의 혼란이 어디인지는 모르겠지만 정적 메서드에서'new World()'를 만들면'new' 인스턴스가 어떻게 든 정적이되지 않습니다. 인스턴스는 인스턴스입니다. 또한'World' 클래스에 정적 메소드를 추가한다고해서 그 클래스의 다른 메소드가 정적 인 것은 아닙니다. – hyde