各种锁的理解

1.公平锁,非公平锁

公平锁: 非常公平,不能插队,必须先来后到

非公平锁: 非常不公平,可以插队,默认是非公平

Lock lock = new ReentrantLock(true);
public ReentrantLock(boolean fair) {
        sync = fair ? new FairSync() : new NonfairSync();
    }

2.可重复锁

synchronized 拿一把锁 会把里面的锁也拿到

public class lock3 {
    public static void main(String[] args) {
        Phone1 phone = new Phone1();
        new Thread(()->{phone.sendSms();},"A").start();

        new Thread(()->{phone.hello();},"B").start();
    }
}

class Phone1{
    public synchronized void sendSms(){
        try {
            TimeUnit.SECONDS.sleep(2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("发短信");
    }
    public synchronized void call(){
        System.out.println("打电话");
    }
    public void hello(){
        System.out.println("hello");
    }
}

lock

public class Lock5 {
    public static void main(String[] args) {
        Phone3 phone = new Phone3();
        new Thread(() -> {
            phone.sendSms();
        }, "A").start();

        new Thread(() -> {
            phone.sendSms();
        }, "B").start();
    }
}

class Phone3 {
    Lock lock = new ReentrantLock();

    public void sendSms() {
        lock.lock();//这里需要注意 , 几个lock()加锁动作就需要对应几个unlock()解锁,一把锁一把钥匙 ,这是和synchronized不一样的地方
        //lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "sendSms 发短信");
            call();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void call() {
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName() + "call 打电话");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

3.自旋锁

不断循环 ,知道成功为止

public class SpinlockTest {
    /**
     * 自定义 自旋锁
     */
    AtomicReference<Thread> atomicReference = new AtomicReference<>();

    public void myLock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "- mylock");
        while (atomicReference.compareAndSet(null, thread)) {

        }
    }

    public void myUnLock() {
        Thread thread = Thread.currentThread();
        System.out.println(Thread.currentThread().getName() + "- myUnLock");
        atomicReference.compareAndSet(thread, null);
    }

    /**
     * 测试
     *
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        SpinlockTest spinlockTest = new SpinlockTest();
        new Thread(() -> {
            spinlockTest.myLock();
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                spinlockTest.myUnLock();
            }
        }, "aa").start();

        TimeUnit.SECONDS.sleep(5);

        new Thread(() -> {
            spinlockTest.myLock();
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                spinlockTest.myUnLock();
            }
        }, "bb").start();
    }
}

4.死锁

public class DeadLock {
    public static void main(String[] args) {
        String resourceA = "resourceA";
        String resourceB = "resourceB";
        new Thread(new MyThread(resourceA,resourceB),"T1").start();
        new Thread(new MyThread(resourceB,resourceA),"T2").start();
    }
}

class MyThread implements Runnable{

    private String resourceA;
    private String resourceB;

    public MyThread(String resourceA, String resourceB) {
        this.resourceA = resourceA;
        this.resourceB = resourceB;
    }

    @Override
    public void run() {
        synchronized (resourceA){
            System.out.println(Thread.currentThread().getName()+"resourceA get resourceB");
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (resourceB){
                System.out.println(Thread.currentThread().getName()+"resourceB get resourceA");
            }
        }
    }
}

 排除死锁的方法:

使用 jps -l 定位进程号

使用 jstack 进程号 找到问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

feng_zi-06

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值