您的位置:首页 > 编程语言 > Java开发

一个简单的java锁Lock的实现

2013-10-17 17:15 399 查看
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();
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息