Java multi-thread programming 7: deadlock (source code)

Source: Internet
Author: User

Java multi-thread programming 7: deadlock (source code)

Source code download
In multi-threaded programming, thread deadlock is also an interesting issue. However, the possibility of deadlock is very small. For this reason, we may not be very familiar with it. But deadlock is not unimportant, because it does exist and will appear in our program at any time. Many of my friends encountered a similar programming question during the interview: using Java to write a deadlock. The problem is not very difficult, but it is really difficult for some people to solve it due to a small number of problems in reality. This article lists the source code of a simple but classic deadlock and explains the cause of the deadlock. I believe that readers will have a better understanding of deadlocks after reading them!
The cause of the deadlock is generally caused by mutual lock wait between two objects. For example, thread 1 first obtains the object lock of object A, and thread 2 first obtains the object lock of object B. Thread 1 requires the object lock of object B at this time, because the object lock of object B is temporarily held by thread 2, so thread 1 can only hold the object lock of object A and wait for thread 2 to release the object lock of object B. At this time, thread 2, which holds the object lock of object B, needs to access the object lock of object a because the object lock of object A is temporarily held by thread 1, therefore, thread 2 can only hold the object lock of object B and wait for thread 1 to release the object lock of object. At this time, the two threads are in the deadlock of waiting for each other. The program enters the Deadlock State. Unless you stop the program manually, it will be permanently deadlocked.
Is it a bit difficult? Hey, if you understand the object lock concept, the cause of the deadlock will be clear at a glance. The object lock is introduced in the previous blog "Java multi-thread programming 6: communication between threads (with source code)". I will not go into details here.
Use the code to describe the problem and view the code demo directly.
Deadlock example-resource source code

Package COM. defonds. deadlock; <br/>/** <br/> * Project name: threaddeadlock <br/> * Class Name: resource <br/> * Description: Resource class, used to represent the data resources that the thread competes with <br/> * Creator: defonds <br/> * Creation Time: 02:01:16 <br/> * modifier: defonds <br/> * modification time: 02:01:16 <br/> * remarks: <br/> * @ version <br/> */<br/> public class resource {</P> <p> private int value; // resource attributes <br/> Public int getvalue () {<br/> return value; <br/>}< br/> Public void setvalue (INT value) {<br/> This. value = value; <br/>}< br/>
Deadlock example-Resource Manager source code

Package COM. defonds. deadlock; <br/>/** <br/> * Project name: threaddeadlock <br/> * Class Name: resourceManager <br/> * class description: resource management class, interface for Resource Data Operations <br/> * Creator: defonds <br/> * Creation Time: 02:04:59 <br/> * modifier: defonds <br/> * modification time: 02:04:59 <br/> * remarks: <br/> * @ version <br/> */<br/> public class ResourceManager {</P> <p>/** <br/> * Two resources managed <br/> */<br/> private resource resourcea = new resource (); <br/> private resource resourceb = new resource (); </P> <p>/** <br/> * Create a new instance ResourceManager. <br/> */<br/> Public ResourceManager () {<br/> This. resourcea. setvalue (0); <br/> This. resourceb. setvalue (0 ); <br/>}</P> <p>/** <br/> * read resources <br/> */<br/> Public int read () {<br/> synchronized (this. resourcea) {<br/> system. out. println (thread. currentthread (). getname () + "the thread gets the resource resourcea object lock"); <br/> synchronized (resourceb) {<br/> system. out. println (thread. currentthread (). getname () + "the thread obtains the resource resourceb object lock"); <br/> return this. resourcea. getvalue () + this. resourceb. getvalue (); <br/>}</P> <p>/** <br/> * Resource rewriting <br/> */< br/> Public void write (int, int B) {<br/> synchronized (this. resourceb) {<br/> system. out. println (thread. currentthread (). getname () + "the thread obtains the resource resourceb object lock"); <br/> synchronized (this. resourcea) {<br/> system. out. println (thread. currentthread (). getname () + "the thread gets the resource resourcea object lock"); <br/> This. resourcea. setvalue (a); <br/> This. resourceb. setvalue (B); <br/>}< br/>
Deadlock example-custom thread source code

Package COM. defonds. deadlock; <br/>/** <br/> * Project name: threaddeadlock <br/> * Class Name: customizethread <br/> * class description: Custom Thread class <br/> * created by: defonds <br/> * created at: 02:29:01, December 26, <br/> * modified: defonds <br/> * modification time: 02:29:01 <br/> * modification remarks: <br/> * @ version <br/> */<br/> public class customizethread extends thread {<br/> private ResourceManager resourcemanger; // Private reference of the resource management class. You can use this reference to read and write resources through its related interfaces. <br/> private int A, B; // data of the resource to be written </P> <p>/** <br/> * Create a new instance customizethread. <br/> * @ Param resourcemanger <br/> * @ Param A <br/> * @ Param B <br/> */<br/> Public customizethread (ResourceManager resourcemanger, int A, int B) {<br/> This. resourcemanger = resourcemanger; <br/> This. A = A; <br/> This. B = B; <br/>}</P> <p>/** <br/> * rewrite Java. lang. thread run method <br/> */<br/> Public void run () {<br/>/** <br/> * to demonstrate the deadlock, here, resources are read and written repeatedly <br/> * the actual service may be read-only once <br/> */<br/> while (true) {<br/> This. resourcemanger. read (); <br/> This. resourcemanger. write (this. a, this. b); <br/>}< br/>
Deadlock example-program entry source code

Package COM. defonds. deadlock; <br/>/** <br/> * Project name: threaddeadlock <br/> * Class Name: deadlockapp <br/> * class description: deadlock program entry <br/> * Creator: defonds <br/> * Creation Time: 02:37:27 <br/> * modifier: defonds <br/> * modification time: 02:37:27 <br/> * modification remarks: <br/> * @ version <br/> */<br/> public class deadlockapp {<br/> Public static void main (string [] ARGs) {</P> <p>/** <br/> * deadlock demo thread initialization <br/> */<br/> ResourceManager = new ResourceManager (); <br/> customizethread customizedthread0 = new customizethread (ResourceManager, 1, 2); <br/> customizethread customizedthread1 = new customizethread (ResourceManager, 2, 4 ); </P> <p>/** <br/> * deadlock demo thread startup <br/> */<br/> customizedthread0.start (); <br/> customizedthread1.start (); <br/>}< br/>
The console output segment of the deadlock occurred when the following code is executed:
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourcea object lock.
The thread-0 thread obtains the resource resourceb object lock.
The thread-0 thread obtains the resource resourceb object lock.
Thread-1 gets the resource resourcea object lock.
The above example shows that the deadlock will inevitably occur once two threads wait for each other.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.