当前位置:网站首页>One interview question a day - the underlying implementation of synchronize and the lock upgrade process
One interview question a day - the underlying implementation of synchronize and the lock upgrade process
2022-06-30 04:34:00 【Garfield cat】
Scope of action
synchronized You can modify static methods 、 Decorated instance method 、 Decorated code block
Method underlying principle
jvm Through... In the method table structure in the method constant pool ACC_SYNCHRONIZED Flag to distinguish whether it is a synchronous method , If it's a synchronous method , The thread will hold monitor, And then execute the method , At the end of the execution ( Whether the execution is normal or abnormal ) Release monitor
The underlying principles of code blocks
utilize monitorenter and monitorexit These two bytecode instructions . They are at the beginning and end of the synchronization block, respectively . When jvm Execute to monitorenter When the command , The current thread is trying to get monitor Ownership of objects , If not locked or held by the current thread , Just lock the counter +1; When executed monitorexit When the command , Lock counter -1; When the lock counter is 0 when , The lock is released . If you get monitor Object failed , The thread is blocked , Until the other thread releases the lock .
Lock escalation
jdk1.6 The default is to open the biased lock ,synchronize When the first thread enters , The default is modified to bias lock , Will be the current thread of ID Update to the... Of the object header makeword The thread of ID in , Add the timestamp of the biased lock and modify the flag of the biased lock to 1
When a biased lock is upgraded to a lightweight lock, there is competition between threads , Threads 1 Delayed exit code block , Threads 2 And get this lock again , And threads 2 adopt cas Spin has never been successful , At this point, it will be upgraded to a lightweight lock , If the thread is spinning 1 After execution, the lock is obtained , At this point, it will not be upgraded to a lightweight lock
Upgrading a lightweight lock to a heavyweight lock is a time when the competition between threads is particularly fierce , There are many threads competing for the same lock , Threads 1 Executing code , The remaining threads are spinning and waiting for the lock , When the number of spins reaches the set maximum, the lock is still not obtained , At this point, it is upgraded to a heavyweight lock , Heavyweight locks block the rest of the waiting threads , When the thread that obtains the lock releases the lock, it wakes up the blocked thread , Open a new round of lock competition .
边栏推荐
- Intern method of string
- FortiGate performs DNAT mapping, and intranet users cannot normally access the mapping
- Learn about threads
- Educoder group purchase suspension box page production
- Salary management system based on servlet+jsp+mysql [source code + database]
- Cheap SSL certificate abroad
- The subsystem implementing transaction persistence in DBMS is ()
- Detailed explanation of cookies and sessions
- Sectigo certificate
- IIS request SSL certificate
猜你喜欢

What is an optocoupler circuit and what should be paid attention to in actual use?

深度融合云平台,对象存储界的“学霸”ObjectScale来了

Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
![[FPGA] IIC读写EEPROM 的实现](/img/6a/36e9355058a90d98cffafcbaa1930b.png)
[FPGA] IIC读写EEPROM 的实现

Process architecture and process management

Share an example of a simple MapReduce method using a virtual machine

Robot slam navigation core technology and practice Season 1: Chapter 0_ Slam development overview

Redis implements SMS login function (I) traditional session login

Slam mapping, automatic navigation and obstacle avoidance based on ROS (bingda robot)

How to repair expired SSL certificates?
随机推荐
Summary of the reasons why transactional doesn't work
基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
Input / output and interrupt technology -- microcomputer Chapter 6 learning notes
Enter the date format string as the production date of the commodity, and enter the shelf life (days); Calculate the number of days until today before the expiration date of the product. 1. Change the
Redis实现短信登入功能(一)传统的Session登入
internship:接口案例实现
Array of small C
IO stream, character read / write, copy
Serializable and Deserialize
Qt Creator 8 Beta2发布
Foreign SSL certificate
Share an example of a simple MapReduce method using a virtual machine
数据链路层详解
FortiGate firewall quick initialization administrator password
OneNote production schedule
IO stream, byte stream read / write copy
2021-07-14
[learn FPGA programming from scratch -52]: high level chapter - FPGA development based on IP core - basic framework for IP core use (taking PLL as an example)
After the win10 system uses the browser to download, the content is moved or deleted without reason