JUC并发编程—锁的理解

锁的理解

1、公平锁与非公平锁

公平锁:不能够插队,必须按照先来后到的顺序执行

非公平锁:可以插队(默认都是非公平锁)

lock锁对应的非公平锁与公平锁的源码

ReentrantLock lock = new ReentrantLock();
public ReentrantLock() {
        sync = new NonfairSync();
    }

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

2、可重入锁

synchronized

package com.zkw.JUC并发编程.lock;

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

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

class Phone{
    public synchronized void sms(){
        System.out.println(Thread.currentThread().getName()+"--> sms()");
        call();
    }
    public synchronized void call(){
        System.out.println(Thread.currentThread().getName()+"--> call()");
    }
}

Lock

package com.zkw.JUC并发编程.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

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

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

class Phone2{
    Lock lock = new ReentrantLock();
    public void sms(){
        lock.lock();
        try {
            System.out.println(Thread.currentThread().getName()+"--> sms()");
            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、自旋锁

注意:是T2在自旋,T1在等待解锁

package com.zkw.JUC并发编程.lock;

import java.util.concurrent.atomic.AtomicReference;

public class SpinLockDemon {
    //int 0
    //Thread = null;
    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);
    }
}
package com.zkw.JUC并发编程.lock;

import java.util.concurrent.TimeUnit;

public class TestSpinLock {
    public static void main(String[] args) throws InterruptedException {
        SpinLockDemon lock = new SpinLockDemon();
        new Thread(()->{
            lock.MyLock();
            try {
                TimeUnit.SECONDS.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                lock.MyUnLock();
            }
        },"T1").start();

        TimeUnit.SECONDS.sleep(2);

        new Thread(()->{
            lock.MyLock();
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.MyUnLock();
            }
        },"T2").start();
    }
}

4、死锁

package com.zkw.JUC并发编程.lock;

import java.util.concurrent.TimeUnit;

public class DeadLockDemon {
    public static void main(String[] args) {
        String LockA = "LockA";
        String LockB = "LockB";
        new Thread(new MyThread(LockA,LockB),"T1").start();
        new Thread(new MyThread(LockB,LockA),"T2").start();

    }


}
class MyThread implements Runnable{
    private String LockA;
    private String LockB;

    public MyThread(String lockA, String lockB) {
        LockA = lockA;
        LockB = lockB;
    }

    @Override
    public void run() {
        synchronized (LockA){
            System.out.println(Thread.currentThread().getName()+" Lock "+LockA+"=>get "+LockB);

            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LockB){
                System.out.println(Thread.currentThread().getName()+" Lock "+LockB+"=>get "+LockA);
            }
        }
    }
}

解决问题 在IDEA中鼠标点击

在这里插入图片描述

1、使用jps -l定位进程号

2、使用jstack 进程号 查看信息

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值