public class Lock {
private volatile boolean isLocked = false;
private Thread lockingThread = null;
public synchronized void lock() throws InterruptedException {
while (isLocked) {
wait();
}
isLocked = true;
lockingThread = Thread.currentThread();
}
public synchronized void unLock() {
if (this.lockingThread != Thread.currentThread()) {
throw new IllegalMonitorStateException("Calling thread has not locked this lock");
}
isLocked = true;
lockingThread = null;
notify();
}
}
一个简单的java锁Lock的实现
最新推荐文章于 2025-05-06 15:46:07 发布