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

Troubleshooting and solution of Java deadlock problem

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "troubleshooting and solutions to Java deadlock problems". 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. Why is there a deadlock?

To solve the Java deadlock, we must get to the bottom of it. Why is there a deadlock? In fact, it can be seen from the definition of deadlock, on the one hand, because there are two or more processes, on the other hand, because there are competitive resources.

2. How to troubleshoot the deadlock in the code?

(1) use jps + jstack

In the windons command window, use jps-l

Use jstack-l 12316

(2) use jconsole

Opening JConsole,JConsole in window is a graphical monitoring tool!

In the windons command window, output JConsole

Select to the tab of the thread

(3) use Java Visual VM

Opening jvisualvm,jvisualvm in window is a graphical monitoring tool!

In the windons command window, output jvisualvm

Still switch to the thread of this TAB, there is an obvious hint!

3. How to avoid deadlocks?

There are three ways to detect and troubleshoot deadlocks, and the more important thing below is how to avoid deadlocks. If the written code can avoid deadlocks, there will be no such troubleshooting process. The best thing is to control the problem from the source, not to fill the hole when you encounter problems later.

I read Alibaba's latest development protocol, which contains instructions for avoiding deadlocks, as follows:

The reason for the deadlock is that two threads try to acquire the same lock in a different order. Therefore, if all threads acquire locks in a fixed order, there will be no lock sequence deadlock in the program.

(1) dynamic locking sequential deadlock

To illustrate with a classic case of money transfer, we know that a transfer is the transfer of funds from one account to another. Before starting the transfer, you first need to obtain the locks of these two account objects to ensure that the balances in the two accounts are updated atomically without breaking some undistorted conditions, such as the account balance cannot be negative.

Conclusion: because we cannot control the order of the parameters in transferMoney, and the order of these parameters depends on the external input. So when two threads call transferMoney at the same time, one thread transfers money from X to Y and the other thread transfers money from Y to X, then they wait for each other to lock, resulting in a deadlock.

Solution: define the order of locks, and acquire locks in this order throughout the application.

Scenario 1: use the System.identityHashCode method, which returns the value returned by Object.hashCode, and the lock order can be determined by some arbitrary method. But in rare cases, two objects may have the same hash value, in which case locking is achieved by locking common variables. So this method is also with the least cost, in exchange for the maximum security.

Option 2: include a unique, immutable value in the Account. For example, account number and so on. Sort the object by this value.

(2) deadlock between collaborating objects

If an external method is called while holding the lock, there will be an activity problem. Another lock may be acquired in this external method (this may cause a deadlock), or the blocking time may be too long, causing other threads to fail to acquire the lock currently held in time.

The scenario is as follows: Taxi represents the taxi object, including the current location and destination. Dispatcher represents the motorcade. When a thread receives an GPS update event to deactivate setLocation, it first updates the location of the taxi and then determines whether it has reached its destination. If it has arrived, it notifies Dispatcher that it needs a new destination. Because setLocation and notifyAvailable are both synchronous methods, the deprecated setLocation thread first acquires taxi's lock, and then acquires Dispatcher's lock. Similarly, threads that deactivate getImage first acquire Dispatcher locks, and then acquire locks for each taxi, and the two threads acquire locks in a different order, which can lead to deadlocks.

Solution: use open deactivation. If you do not need to hold a lock when you call a method again, then the call is called open deactivation. This call can effectively avoid deadlocks and is easy to analyze thread safety.

This is the end of the content of "troubleshooting and Solutions to Java deadlock problems". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Development

Wechat

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

12
Report