Reputation: 21616
How to use WriteLock
on a static
method? this is what I have got:
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();
}
Is it the right way to define m_unitWriteLock
as static? what you recommend for such a situation? tnx.
Upvotes: 0
Views: 453
Reputation: 1427
Yes, why not?
If you method is static, the lock must be static as well. The question is: Does the method need to be static? But that depends on the problem you are going to solve.
Upvotes: 2