코드를 첨부했습니다. 이게 무슨 뜻입니까? synchronized (m) .. ?? 왜 우리가 그걸 사용해야하니 .. ?? synchronized (this) & synchronized (m) ...의 차이점은 무엇입니까?동기화 (인스턴스 필드)를 말할 때의 의미는 무엇입니까?
class Waiter implements Runnable {
Message m;
public Waiter(Message m) {
this.m = m;
}
@Override
public void run() {
String name = Thread.currentThread().getName();
synchronized (m) {
try {
System.out.println("Waiting to get notified at time " +System.currentTimeMillis());
m.wait();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.out.println("Waiter thread notified at time "+System.currentTimeMillis());
System.out.println("Message processed ");
}
}
}
'synchronized (m)''->'Message' 객체를 잠급니다. –
메시지 객체를 잠그는 이유는 무엇입니까 ?? 우리가 얻을 수있는 용도는 무엇인가 .. ?? 대신, 왜 우리는 synchronized (this)를 사용하지 말아야합니까? –
@surendhar_s java의 각 객체는 'intrinsic lock'이라는 잠금을 가지고 있습니다. 'synchronized (this)'를 사용하면 클래스 Waiter의 현재 인스턴스와 관련된 잠금을 사용하게됩니다. –