**背景:**在多线程编程中,为了避免共享数据的不一致性,对共享数据进行操作的时候必须要加锁,在java中可以使用synchronized关键字实现排他锁,但是使用该关键字有2大缺陷:
1.无法控制线程的阻塞时长;
2.阻塞是不可被中断的.
为了避免出现以上情况,我们可以进行自定显示锁,实现自定义阻塞时长并且是可中断的.代码示例如下:
1.定义接口
package thread;
import java.util.List;
import java.util.concurrent.TimeoutException;
public interface Lock {
void lock() throws InterruptedException;
void lock(Long time) throws InterruptedException,TimeoutException;//毫秒
void unlock();
List<Thread> getBlockedThreads();//获取阻塞的线程
}
2.锁的实现
package thread;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeoutException;
public class BooleanLock implements Lock {
private Thread currentThread;
private boolean locked;
private final List<Thread> blockedThreads = new ArrayList<>();
@Override
public void lock() throws InterruptedException {
synchronized (this){
System.out.println("线程"+ Thread.currentThread().getName()+"进入lock同步代码块...");
while (locked){
blockedThreads.add(Thread.currentThread());
System.out.println("线程"+Thread.currentThread().getName()+"等待获取锁对象: "+this);
this.wait();//放弃锁的所有权
//在同步代码块中,所有的线程要想继续往下执行必须要获得this.monitor的锁
}
blockedThreads.remove(Thread.currentThread());
this.locked = true;//获取锁
this.currentThread = Thread.currentThread();//获取锁的线程
System.out.println("线程"+this.currentThread.getName()+"获得锁...");
}
}
/**
* 获取锁的阻塞时长
* @param time 单位毫秒
* @throws InterruptedException
* @throws TimeoutException
*/
@Override
public void lock(Long time) throws InterruptedException, TimeoutException {
synchronized (this){
if (time <= 0L){
this.lock();
}else {
long remainTime = time;
long endTime = System.currentTimeMillis() + remainTime;
while (this.locked){
if (remainTime <= 0L){
throw new TimeoutException("can not get lock during "+ time +"ms");
}
if (!blockedThreads.contains(Thread.currentThread())){
blockedThreads.add(Thread.currentThread());
}
this.wait(remainTime);//等待时间
remainTime = endTime -System.currentTimeMillis();
}
blockedThreads.remove(Thread.currentThread());
this.locked = true;
this.currentThread = Thread.currentThread();
}
}
}
@Override
public void unlock() {
synchronized (this){
if (Thread.currentThread() == this.currentThread){
this.locked = false;
System.out.println("线程" + this.currentThread.getName()+ "释放锁: " + this);
this.notifyAll();//唤醒所有的线程,去争抢this.monitor锁,抢到了进行入runnable状态,抢不到继续Blocked
}
}
}
@Override
public List<Thread> getBlockedThreads() {
return Collections.unmodifiableList(this.blockedThreads);
}
}