JUC(11)各种锁的理解(公平锁、可重入锁、自旋锁、死锁)

网友投稿 684 2022-09-03

JUC(11)各种锁的理解(公平锁、可重入锁、自旋锁、死锁)

JUC(11)各种锁的理解(公平锁、可重入锁、自旋锁、死锁)

文章目录

​​1、公平锁、非公平锁​​​​2、可重入锁​​​​3、自旋锁​​​​4、死锁​​

1、公平锁、非公平锁

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

2、可重入锁

拿到外边的锁、就会自动拿到里边的锁。

package com.lock;/** * synchronized */public class Demo1 { 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.lock;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * synchronized */public class Demo1 { public static void main(String[] args) { Phone phone = new Phone(); new Thread(()->{ phone.sms(); },"A").start(); new Thread(()->{ phone.sms(); },"B").start(); }}class Phone{ 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、自旋锁

spinlock

自己写自旋锁

package com.lock;import java.util.concurrent.atomic.AtomicReference;import java.util.concurrent.atomic.AtomicStampedReference;public class SpinLockDemo { AtomicReference 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.lock;import java.util.concurrent.TimeUnit;public class Test { public static void main(String[] args) { SpinLockDemo spinLockDemo = new SpinLockDemo(); new Thread(()->{ spinLockDemo.mylock(); try { TimeUnit.SECONDS.sleep(3); } catch (Exception e) { e.printStackTrace(); } finally { spinLockDemo.myunlock(); } },"A").start(); new Thread(()->{ spinLockDemo.mylock(); try { System.out.println(); } catch (Exception e) { e.printStackTrace(); } finally { spinLockDemo.myunlock(); } },"B").start(); }}

4、死锁

package com.lock;import java.util.concurrent.TimeUnit;public class DeadLockDemo { 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){ this.lockA = lockA; this.lockB =lockB; } @Override public void run() { synchronized (lockA){ System.out.println(Thread.currentThread().getName()+"lock:"+lockA+"==>"+lockB); try { TimeUnit.SECONDS.sleep(2); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB){ System.out.println(Thread.currentThread().getName()+"lock:"+lockB+"==>"+lockA); } } }}

测试

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

2、使用​​jstack 进程号​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:PHP 使用Echarts生成数据统计报表详解
下一篇:JUC(8)Stream流式计算
相关文章

 发表评论

暂时没有评论,来抢沙发吧~