Emergence and solution of deadlock in Java multithreading

What is deadlock?

Deadlock is a situation where multiple threads are blocked at the same time, and one or all of them are waiting for a resource to be released Because the thread is blocked indefinitely, the program cannot run normally The image is: a treasure needs two keys to open. At the same time, two people came. They each have a key, but both sides waited for the other party to hand over the key to open the treasure. No one released his key In this way, the two people remained deadlocked until the developers found out the situation

The root cause of deadlock is the improper use of "synchronized" keyword to manage thread access to specific objects The function of "synchronized" keyword is to ensure that only one thread is allowed to execute a specific code block at a certain time. Therefore, the thread allowed to execute must first have exclusive access to variables or objects When a thread accesses an object, it locks the object, and this lock causes other threads that also want to access the same object to be blocked until the first thread releases the lock it has placed on the object

If you don't know much about synchronized, please click here

for instance

Deadlocks occur mostly when you don't know Let's take an example to see what a deadlock is

1. Synchronized nesting

Synchronized keyword can ensure synchronization when multiple threads access the synchronized modified method When thread a accesses this method, thread B also accesses this method. At this time, thread B will block and wait for thread a to execute before accessing it Here we need to use the synchronization lock held by synchronized Specifically, the code:

Let's analyze. When threada starts to execute the run method, it will first hold the object lock locala, and then sleep for 2 seconds. At this time, threadb also starts to execute the run method, which holds the localb object lock When threada runs to the second synchronization method, it finds that the object lock of localb cannot be used (threadb does not release the localb lock), threada stops here and waits for the localb lock Then, threadb also executes the second synchronization method. When accessing the locala object lock, it finds that the locala has not been released (threada has not released the locala lock), and threadb also stops here waiting for the locala lock to be released In this way, neither thread can continue to execute and enter the deadlock state See the operation results:

When there is no deadlock, four logs should be printed. There is an obvious deadlock here

Causes of deadlock

When we know under what circumstances deadlock will occur and what is deadlock, we should try our best to avoid this misunderstanding when writing code The following four conditions must be met to generate a deadlock. As long as any one of these conditions is not true, the deadlock will not occur

Deadlock resolution

To be honest, you have to pay attention when writing your own code to avoid deadlock I quote other people's solutions here, but I don't understand these solutions very well. I'm too vague and don't have specific examples

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>