public class Lock{
private boolean isLocked = false;
private Thread lockingThread = null;
public synchronized void lock() throws InterruptedException{=
//自旋锁
while(isLocked){
wait();
}
//线程调用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 = false;
lockingThread = null;
notify();
}
}