Hence the program will run for an infinite time. We can observe that both threads could not execute completely. Inside the synchronization block of thread T1, we are requesting for the resource R2, which is locked by the thread T2, and in the synchronization block of the thread T2, we are requesting for the resource R1, which is locked by the thread T1. Similarly, in thread T2, we lock the resource R2. In thread T1, we are creating a run method to lock the resource R1 using the synchronized method. In the main method, we are creating two threads, T1 and T2. In the above code snippet, first, we are declaring the strings R1 and R2 as a resource. Let us implement the example discussed above-involving threads T1 and T2 and resources R1 and R2. This situation is an example of a deadlock in Java.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |