自定义排他锁

**背景:**在多线程编程中,为了避免共享数据的不一致性,对共享数据进行操作的时候必须要加锁,在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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值