手写一个公平锁

公平锁:先调用加锁的线程优先得到锁,通过队列来实现

 

package MyLock;

import java.util.ArrayList;
import java.util.List;

public class FairLock {

    private boolean isLocked = false;
    private Thread lockingThread = null;
    private List<QueueObject> waitingThreads = new ArrayList<>();
    //加锁操作
    //当前线程获得锁的条件:
    //1、当前锁没有人获取 2、当前线程在链表的头部
    public void lock() throws InterruptedException {
        QueueObject queueObject = new QueueObject();//每次调用一次lock就会新建一个临时对象标记当前线程
        synchronized (waitingThreads) {
            waitingThreads.add(queueObject);
            //不管有没有获得FairLock对象锁,先将监视器压入队列。
        }
        while(true){
            synchronized (this) {
                if(!isLocked && queueObject.equals(waitingThreads.get(0))){

                    isLocked = true;
                    lockingThread = Thread.currentThread();
                    return;
                }
            }
            //如果FairLock锁已被占,或者当前线程不在队列头,那么调用监视器的doWait()等待
            try {
                queueObject.doWait();
                //如果一个线程从wait()返回,那么它必然在队列头
            } catch (InterruptedException e) {
                synchronized (waitingThreads) {
                    waitingThreads.remove(queueObject);//出现异常,及时收尾
                }
                throw e;
            }

        }

    }

    public  synchronized void unlock() {
        if (this.lockingThread != Thread.currentThread()) {
            throw new IllegalMonitorStateException("Calling thread has not locked this lock");
        }
        isLocked = false;
        lockingThread = null;
        if (!waitingThreads.isEmpty()) {
            waitingThreads.get(0).doNotify();
            synchronized (waitingThreads){
                waitingThreads.remove(0);
            }
        }
    }

    public static void main(String[] args) {
        FairLock fairLock = new FairLock();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    fairLock.lock();
                    System.out.println("线程1执行...");
                    fairLock.unlock();
                }catch (Exception e){

                }
            }
        }).start();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    fairLock.lock();
                    System.out.println("线程2执行...");
                    fairLock.unlock();
                }catch (Exception e){

                }
            }
        }).start();

        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    fairLock.lock();
                    System.out.println("线程3执行...");
                    fairLock.unlock();
                }catch (Exception e){

                }
            }
        }).start();
    }


}
//监听器对象
class QueueObject {
    private boolean isNotified = false;

    public synchronized void doWait() throws InterruptedException {
        /*
        使用while()形式封装监听器的wait()和notify()的好处多多:
        1、防止wait()错过信号
        2、防止假唤醒(spurious wakeups)
        3、多个线程等待相同的信号
        */
        while (!isNotified) {
            this.wait();
        }
        isNotified = false;
    }

    public synchronized void doNotify() {
        this.isNotified = true;
        this.notify();
    }
    @Override
    public boolean equals(Object o) {
        return this == o;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值