【发布时间】:2016-10-04 22:44:47
【问题描述】:
假设我有这个课程:
public class Status {
private int x;
// monitor lock?
public Object myLock = new Object();
public Status(int x) {
this.x = x;
}
public int checkVar() {
return x;
}
public int incrementVar() {
++x;
}
}
然后我有一个这样的线程类:
public class MyThread implements Runnable {
public void run() {
// Is this how to acquire monitor lock of Status class?
synchronized (statusInstance.myLock) {
statusInstance.checkVar();
statusInstance.incrementVar();
}
}
}
你是这样获取另一个类的监视器锁的吧?
【问题讨论】:
-
你也可以使用 synchronized (statusInstance)
标签: java multithreading concurrency locking