Back
Close

Computing with Data

elgeish
33K views

Reentrant Locks - Part I

This kind of locking is useful when one method acquires the lock and calls another that also acquires said lock; for example:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class ReentrantLockExample {
private final Lock lock = new ReentrantLock();
public void foo() {
lock.lock();
try {
// ...
bar();
} finally {
lock.unlock();
}
}
public void bar() {
lock.lock();
try {
// ...
} finally {
lock.unlock();
}
}
}
public class Main {
public static void main(String args[]) throws Exception {
new ReentrantLockExample().foo();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants