0

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.

tokhi
  • 21,044
  • 23
  • 95
  • 105

1 Answers1

2

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.

roehrijn
  • 1,387
  • 1
  • 11
  • 20