tokhi
tokhi

Reputation: 21616

Java - Reentrant WriteLock on static methods

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

Answers (1)

roehrijn
roehrijn

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

Related Questions