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 进程号 查看信息

Guess you like

Origin blog.csdn.net/fgets__/article/details/120794766