如何在static方法上使用WriteLock?这是我得到的:
m_unitLock = new ReentrantReadWriteLock();
m_unitReadLock = m_unitLock.readLock();
m_unitWriteLock = m_unitLock.writeLock()
static List<unit> units = new ArrayList<Unit>();
...
public static addUnit(){
m_unitWriteLock.lock(); // can not use this inside a static method
units.add(unit);
m_unitWriteLock.unlock();
}这是将m_unitWriteLock定义为静态的正确方式吗?对于这种情况,你有什么建议?tnx。
发布于 2013-08-29 23:49:39
是啊,有何不可?
如果你的方法是静态的,那么锁也必须是静态的。问题是:这个方法需要是静态的吗?但这取决于你要解决的问题。
https://stackoverflow.com/questions/18515412
复制相似问题