仅提供学习,侵权必删,如有错误,敬请告知

一、公平锁/非公平锁

package suo;import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class Gongpingsuo {private static Lock lock = new ReentrantLock(true); //当为true是 是公平锁,默认是非公平锁private static volatile int count = 100;public static void lock() {lock.lock();                            //添加锁count--;System.out.println("剩余:"+count);lock.unlock();                           //释放锁}public static void main(String[] args) {for (int i = 0; i < 100; i++) {new Thread(()-> {lock();}).start();}}
}

二、死锁

package suo;public class Sisuo {public static void AA() {synchronized ("AA") {System.out.println("AA");
//          try {//              "AA".wait();
//          } catch (InterruptedException e) {//              // TODO Auto-generated catch block
//              e.printStackTrace();
//          }synchronized ("BB") {System.out.println("AABB");}}}public static void BB() {synchronized ("BB") {System.out.println("BB");synchronized ("AA") {System.out.println("BBAA");
//              "AA".notifyAll();}}}public static void main(String[] args) {Thread AA = new Thread("AA") {@Overridepublic void run() {// TODO Auto-generated method stubAA();}};Thread BB = new Thread("BB") {@Overridepublic void run() {// TODO Auto-generated method stubBB();}};AA.start();BB.start();}
}

三、悲观锁

package Suo9;public class Beiguansuo {private static volatile int count = 100;public static synchronized void temp() {count--;System.out.println("剩余:"+count);}public static void main(String[] args) {for (int i = 0; i < 100; i++) {new Thread(()-> {temp();}).start();}}
}

四、乐观锁

package Suo9;import java.util.concurrent.atomic.AtomicInteger;public class Leguansuo {private static AtomicInteger atomicInteger = new AtomicInteger(0);public static void Add() {atomicInteger.incrementAndGet();}public static void Del() {atomicInteger.decrementAndGet();}public static void main(String[] args) throws InterruptedException {for (int i = 0; i < 100; i++) {Thread thread = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubAdd();}};thread.start();thread.join();}for (int i = 0; i < 100; i++) {Thread thread = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubDel();}};thread.start();thread.join();}System.out.println("当前值:"+atomicInteger.get());}
}

五、同步锁

package Suo8;public class Tongbusuo {private static int count = 100;public synchronized static void name() {count--;System.out.println("剩余数:"+count);}public static void main(String[] args) {for (int i = 0; i < 100; i++) {Thread thread = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubname();}};thread.start();}}
}

六、互斥锁

package Suo9;import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;public class Huchisuo {private static Lock lock = new ReentrantLock();private static volatile int count = 2;public static void temp() {lock.lock();count--;System.out.println("剩余:"+count);lock.unlock();}public static void main(String[] args) {Thread t1 = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubtemp();}};Thread t2 = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubtemp();}};t1.start();t2.start();}
}

七、可重入锁

package Suo9;public class Kechongrusuo {public static void AA() {System.out.println("AA");BB();}public static void BB() {System.out.println("BB");}public static void main(String[] args) {Thread thread = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubAA();}};thread.start();}
}

八、读写锁

package Suo9;import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;public class Duxiesuo {private static ReentrantReadWriteLock lock = new ReentrantReadWriteLock();private static Map<String, Object> map = new ConcurrentHashMap<String, Object>();public static void Xie(String Keys,String Value) {lock.writeLock().lock();System.out.println("我正在写:"+Keys);map.put(Keys, Value);System.out.println("我写完了!下一个");lock.writeLock().unlock();}public static void Du(String Keys) {lock.readLock().lock();System.out.println("我正在读取文件");System.out.println("我读完了:"+map.get(Keys));lock.readLock().unlock();}public static void main(String[] args) {for (int i = 0; i < 10; i++) {int temp = i ;Thread thread = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubXie(temp+"",temp+"");}};thread.start();}for (int i = 0; i < 10; i++) {int temp = i ;Thread thread = new Thread() {@Overridepublic void run() {// TODO Auto-generated method stubDu(temp+"");}};thread.start();}}
}

九、自旋锁

package Suo9;import java.util.concurrent.atomic.AtomicReference;public class Zixuansuo {private static AtomicReference<Thread> atomicReference = new AtomicReference<Thread>();public static void lock() throws InterruptedException {Thread thread = Thread.currentThread();System.out.println(thread.getName());while (!atomicReference.compareAndSet(null, thread)) {System.out.println(thread.getName()+"正在自我循环访问");Thread.sleep(1000);}}public static void unlock() {Thread thread = Thread.currentThread();atomicReference.compareAndSet(thread, null);System.out.println(thread.getName()+"我已释放");}public static void main(String[] args) {Thread AA = new Thread(()-> {try {lock();Thread.sleep(5000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}unlock();},"AA");AA.start();try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}Thread BB = new Thread(()-> {try {lock();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}unlock();},"BB");BB.start();}
}

JAVA-----锁机制相关推荐

  1. 转 : 深入解析Java锁机制

    深入解析Java锁机制 https://mp.weixin.qq.com/s?__biz=MzU0OTE4MzYzMw%3D%3D&mid=2247485524&idx=1&s ...

  2. 面试必会系列 - 1.5 Java 锁机制

    本文已收录至 github,完整图文:https://github.com/HanquanHq/MD-Notes 面试必会系列专栏:https://blog.csdn.net/sinat_424833 ...

  3. Java锁机制,synchronized和lock详解。

    Java锁机制详解 1.java各种锁详解 1.1 公平锁 vs 非公平锁 公平锁:是指多个线程按照申请锁的顺序来获取锁,线程直接进入队列中排队,队列中的第一个线程才能获得锁.类似排队打饭,先来后到. ...

  4. Java锁机制学习笔记——synchronized 和 Lock

    为什么80%的码农都做不了架构师?>>>    synchronized synchronized关键字相信大家都不陌生了,作为java关键字,它可以帮助我们实现对方法的加锁同步.它 ...

  5. 一文看懂Java锁机制

     作者:VectorJin https://juejin.cn/post/6844904036026548237 背景知识 指令流水线 CPU的基本工作是执行存储的指令序列,即程序.程序的执行过程实际 ...

  6. Java锁机制了解一下

    前言 回顾前面: 多线程三分钟就可以入个门了! Thread源码剖析 多线程基础必要知识点!看了学习多线程事半功倍 只有光头才能变强! 本文章主要讲的是Java多线程加锁机制,有两种: Synchro ...

  7. java 重量级锁_轻量级锁和重量级锁的区别分别有哪些?java锁机制教程

    Java中有着各种锁机制,今天我们要说的就是其中两种状态,轻量级锁与重量级锁,小伙伴们知道它们的区别分别有哪些吗?下面来了解一下吧. 首先我们了解一下有哪些锁状态吧 锁的状态总共有四种:无锁状态.偏向 ...

  8. Java 锁机制(synchronized 与 Lock)

    在java中,解决同步问题,很多时候都会使用到synchronized和Lock,这两者都是在多线程并发时候常使用的锁机制. synchronized是java中的一个关键字,也就是说是java内置的 ...

  9. 阿里P8大牛总结的Java锁机制入门笔记,堪称教科书式天花板

    前言 锁机制无处不在,锁机制是实现线程同步的基础,锁机制并不是Java锁独有的,其他各种计算机语言中也有着锁机制相关的实现,数据库中也有锁的相关内容.这篇文章就是从Java入手,深入学习.理解Java ...

  10. 深入解析Java锁机制

    作者:家琪,美团点评后端工程师.2017 年加入美团点评,负责美团点评境内度假的业务开发. 来自:美团技术团队(meituantech) 前言 Java提供了种类丰富的锁,每种锁因其特性的不同,在适当 ...

最新文章

  1. DJANGO获取用户访问IP
  2. 【opencv】15.H265Decoder解码h265为cv::Mat完整代码
  3. Windows 恢复环境(Windows RE模式)
  4. 怎么判断一个字符串的最长回文子串是否在头尾_回文自动机入门
  5. Python字典的操作与使用
  6. 今天终于结束了考试,不知道结果
  7. 洛谷 2051 [AHOI2009] 中国象棋
  8. android游戏源码
  9. Windows NT 5.1,Windows NT 6.0,Windows NT 6.1的概念
  10. 有道云笔记分享_写完笔记后干啥 有道云笔记分享技巧
  11. UIKit Dynamics入门
  12. 竹云+巨杉丨互信认证 安全可靠
  13. 人们在居住时关注的不是[空间],而是[空间感]。好的设计/布局=额外赠送了居住面积。
  14. 程序员复工后被裁,600万房21000房贷无力偿还,给年轻人3点忠告
  15. unity泛型单例模式Singleton
  16. AI美图相机原型(智能P图、AI换脸)
  17. 37种传感器(六)之声音传感器模块+Stduino NanoUNO
  18. 美团NLP以及知识图谱文章提炼
  19. LAP, UTKFace,webface, morph II 人脸数据集
  20. 由GPS定位的经纬度转换成百度地图经纬度坐标

热门文章

  1. wor2007添加分节符
  2. 什么是SSL协议,浅谈SSL协议。
  3. 如何有效的做好线上引流?如何自己做引流推广?
  4. 健身产品如何线上引流,健身行业如何线上引流?
  5. Dev C++报错找不到zlib1.dll解决办法以及调试入门
  6. attention机制、self-attention、channel attention、spatial attention、multi-head attention、transformer
  7. python - 求商和余数“/“ “//“ “%“运算符的区别
  8. 7-1 循环-古角猜想 (20 分)
  9. wrcoef2函数_matlab中二维小波变换部分函数
  10. 计算机系统时间的修复,电脑时间总是不对,小编教你如何恢复正常