第一部分:是接口定义超时错误;
package com.lsz.thread.lock;
import java.util.Collection;
public interface Lock {
class TimeOutException extends Exception {
public TimeOutException(String message) {
super(message);
}
}
void lock() throws InterruptedException;
void lock(long ms) throws InterruptedException, TimeOutException;
void unlock();
Collection<Thread> getBlockedThread();
int getBlockSize();
}
第二部分:是接口实现类,实现超时锁;
package com.lsz.thread.lock;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Optional;
/**
* 实现显示锁接口
*/
public class BooleanLock implements Lock {
//false 说明锁已释放,可以去抢了;
//true 说明锁没有释放
private boolean isLock;
private Thread currentThread;
private Collection<Thread> blockedThreadCollection = new ArrayList<>();
public BooleanLock(