In this section, you will learn about deadlock with an example.
Deadlock
A situation where a thread is waiting for an object lock that holds by second thread, and this second thread is waiting for an object lock that holds by first thread, this situation is known as Deadlock.
Lets see a situation in the diagram shown below where the deadlock condition is occurred :
In this diagram two threads having the Printing & I/O operations respectively at a time. But Thread1 need to printer that is hold up by the Thread2, likewise Thread2 need the keyboard that is hold up by the Thread1. In this situation the CPU becomes ideal and the deadlock condition occurs because no one thread is executed until the hold up resources are free.
The following program demonstrates the deadlock situation:
public class DeadDemo{
|
Output of the program is:
C:\j2se6\thread>javac DeadDemo.java C:\j2se6\thread>java DeadDemo DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock LockDead LockDead LockDead LockDead LockDead LockDead LockDead DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock DeadLock ......... ......... C:\j2se6\thread> |