8. Fair lock and unfair lock of Java lock

Why is the algorithm so difficult 2021-02-23 18:39:48
fair lock unfair lock java


Concept

Fair lock : It means that multiple threads acquire locks according to the order in which they apply for locks . It's like queuing up to buy food , first come , first served , First come, first served , It's fair , That's the queue

Not fair lock : The order in which multiple threads acquire locks , It's not in the order of applying for locks . It is possible that the thread that applied first gets the lock before the thread that applied first , In a highly concurrent environment , It may cause priority reversal , Or hungry threads ( That is, a thread can't wait for the lock )

establish

Under contract ReentrantLock The creation of can specify the boolean Type to get fair lock or unfair lock , Default is unfair lock

Lock lock = new ReentrantLock(true);
/*
Create a reentrant lock .true Fair lock ,false It means unfair lock . The default is false.
*/

difference

Fair lock : In a concurrent environment , Each thread will first check the waiting queue maintained by the lock when acquiring the lock , If it is empty , Or wait for the first one in the queue . Take the lock , Otherwise it will join the waiting queue , In the future according to FIFO The rules of get themselves from the queue .

Not fair lock : Unfair locks are rude , Come up and try to own the lock , If the attempt fails , We'll use a fair lock .

tips:

The advantage of unfair lock is that throughput is larger than fair lock , about synchronized for , It's also a kind of unfair lock .

版权声明
本文为[Why is the algorithm so difficult]所创,转载请带上原文链接,感谢
https://javamana.com/2021/02/20210223183529895O.html

  1. JAVA的函数式接口
  2. JAVA里的元祖类
  3. JQuery Gantt package to create a new ASP.NET project
  4. Binary conversion of Unicode code (Java)
  5. The functional interface of Java
  6. Yuanzu class in Java
  7. Java中的CPU占用高和内存占用高的问题排查
  8. HashMap连环问你能答出几道?
  9. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  10. java中的反射和代理
  11. Troubleshooting of high CPU and memory usage in Java
  12. How many questions can you answer?
  13. IntelliJ idea can also draw mind maps. It's really the strongest ide!
  14. Reflection and proxy in Java
  15. Java中的CPU占用高和内存占用高的问题排查
  16. Linux OOM(out of memory)
  17. mysql 自定义函数因参数名称报错
  18. Troubleshooting of high CPU and memory usage in Java
  19. Linux OOM(out of memory)
  20. MySQL user defined function error due to parameter name
  21. echarts-gl 3D 地图柱状图可视化GDP
  22. Visualization of histogram of ecarts GL 3D map
  23. 金三银四如何应对Redis面试,一文深入Redis实战实践!
  24. 阿里资深架构师定制金三银四面试整理出来的一份Java核心知识点.pdf
  25. 为什么Java开发工程师工资高,却很多人想转行?
  26. How to deal with the interview of redis!
  27. Ali senior architect customizes a Java core knowledge point sorted out in the interview of golden, silver and four.pdf
  28. Why do java development engineers have high salaries, but many people want to change careers?
  29. 用注解开发SpringMVC
  30. Developing spring MVC with annotations
  31. 编译redis报错/deps/hiredis/libhiredis.a解决
  32. Compile redis report error / DEPs / hirredis / libhirredis. A solution
  33. 用注解开发SpringMVC
  34. Developing spring MVC with annotations
  35. Spring学习笔记-01
  36. Centos安装和卸载docker
  37. Spring learning notes-01
  38. Installing and uninstalling docker in CentOS
  39. Java基础-异常
  40. Java基础-反射
  41. Java基础-继承
  42. k8s部署 (进行中)
  43. Hive-常见调优方式 && 两个面试sql
  44. 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
  45. Java basics exception
  46. Java Basics - Reflection
  47. Java Basics - inheritance
  48. K8s deployment (in progress)
  49. Hive common tuning methods & two interview SQL
  50. The loading phase of beandefinition (XML file)
  51. 死磕Spring之IoC篇 - BeanDefinition 的加载阶段(XML 文件)
  52. Hive-常见调优方式 && 两个面试sql
  53. The loading phase of beandefinition (XML file)
  54. Hive common tuning methods & two interview SQL
  55. iconv文件编码转换由windows文件放到linux下
  56. The code conversion of iconv file is put into Linux by windows file
  57. SpringBoot2+intellij IDEA开发前环境准备
  58. Preparation of pre development environment for springboot2 + IntelliJ idea
  59. Docker私有仓库部署
  60. Docker private warehouse deployment