For thread synchronization.
Both methods MUST ALWAYS be called from within a statement synchronizing on this object, such as:
final Lock lock = new Lock();
synchronized (lock) {
lock.lock();
try {
... (do whatever needs be synchronized on this lock) ...
} catch (Exception e) {
e.printStackTrace();
}
lock.unlock();
}
The advantage of using this class as opposed to a simple synchronized statement is that the lock may be set and unset from different synchronized blocks. For example:
final Lock lock = new Lock();
synchronized (lock) {
lock.lock();
// Do something
}
// Exit synchronized block, wait for other events to happen
// ...
// Enter again and unlock:
synchronized (lock) {
try {
... (do whatever needs be synchronized on this lock) ...
} catch (Exception e) {
e.printStackTrace();
}
lock.unlock();
}