If you're very keen on using a Lock
, you need to choose a Lock
implementation as you cannot instantiate interfaces.
As per the docs
You have 3 choices:
You're probably looking for the ReentrantLock
possibly with some Condition
s
This means that instead of Lock l = new Lock();
you would do:
ReentrantLock lock = new ReentrantLock();
However, if all you're needing to lock is a small part, a synchronized block/method is cleaner (as suggested by @Leonidos & @assylias).
If you have a method that sets the value, you can do:
public synchronized void setValue (var newValue)
{
value = newValue;
}
or if this is a part of a larger method:
public void doInfinite ()
{
//code
synchronized (this)
{
value = aValue;
}
}