Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the lock optimization method of Java virtual machine?

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "what is the lock optimization method of Java virtual machine". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Lock bias

Lock bias is an optimization method for locking operation.

If a thread acquires a lock, the lock enters biased mode. When this thread requests the lock again, there is no need to do any synchronization. This saves a lot of lock application operations, thus improving the performance of the program.

Therefore, for situations where there is almost no lock competition, the preferred lock has a red optimization effect, because it is very likely that the same lock is requested by the same thread many times in a row. However, for the situation where the lock competition is fierce, the effect is not good. Because in highly competitive situations, it is most likely that each time a different thread requests the same lock. Click here to learn about several common locks.

2. Lightweight lock

If the lock bias fails, that is, the thread of the last requested lock is not the same as this thread. A failure to bias locks means that synchronization cannot be avoided. At this point, the virtual machine does not immediately suspend the thread. He will use an optimization method that becomes a lightweight lock.

The operation of lightweight lock is also very convenient, it simply takes the object head as a pointer to the inside of Chiyou's thread stack to determine whether a thread holds an object lock. If the thread succeeds in obtaining a lightweight lock, it can successfully enter the critical section. If the lightweight lock fails, it means that another thread competes for the lock first, and the lock request of the current thread expands to a heavy lock. Click here to learn about several common locks.

3. Optional lock

After the lock expansion, the virtual machine will make a last-ditch effort-optional lock in order to prevent the thread from actually hanging at the operating system level. Since the current thread is temporarily unable to acquire the lock, it is unknown when the lock will be acquired. Maybe after a few clock cycles of CPU, you can get the lock. If so, simply suspending a thread can be a costly operation, so the system makes a bet: it assumes that the thread can get the lock in the near future.

So the virtual machine lets the current thread do an empty loop, and after several loops, if the lock can be obtained, it will successfully enter the critical area. If the lock is not yet available, the thread will actually be suspended at the operating system level.

4. Lock elimination

Lock elimination is a more thorough lock optimization. When the Java virtual machine compiles in JIT, it scans the running context to remove locks that can't compete for shared resources. Through lock elimination, meaningless lock request time can be saved.

In the following case, we use vector, while vector uses synchronize request locks internally.

Public String [] createStrings () {

Vector v = new Vector ()

For (int iTuno Bandi)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report