Deadlock

In this section, you will learn about deadlock with an example.


 

Ads

Tutorials   
Threading In Java Example Thread Life Cycle Example in java What Is Thread In Java? Creating multiple Threads Daemon Threads Synchronization in java with example Thread priority in java Threads in Java Java Multithreading Example Thread Synchronization in Java Overview of Networking through JAVA,How to retrieve URL information URL in term of Java Network Programming Overview of Networking through JAVA,Convert URI to URL Socket and ports Server Sockets Overview of Networking through JAVA,Find the Host name in reverse of given IP address Print the URL of a URLConnection Networking in Java Computer Networking Overview of Networking through JAVA,Getting the Local port Overview of Networking through JAVA Overview of Networking through JAVA Overview of Networking through JAVA,Getting list of Local Interfaces on a machine Overview of Networking through JAVA Overview of Networking through JAVA,Find Your Host Name Overview of Networking through JAVA,To retrieve the IP address from Host Name, vice-versa Open a URLConnection to specific website address Return the MIME Header Construct a DatagramSocket on an unspecified port Datagram in network environment Construct a DatagramPacket to receive data Client-Server Architecture Thread Priorities Synchronized Threads Multithreading in Java Interthread Communication Creation of MultiThreads Threading in Java Thread Priorities Inter-Thread Communication Synchronized Threads Creation of Multiple Threads Threading in Java Daemon Threads Deadlock Synchronization Life Cycle of Threads Thread Constructors Thread Creation Overview of Thread
Ads

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{
  public static void main(String args[]){
  String s1="Dead";
  String s2="Lock";
  MyThread1 m=new MyThread1(s1,s2);
  MyThread2 m1=new MyThread2(s1,s2);
  }
}

class MyThread1 extends Thread{
  String s1;
  String s2;
  MyThread1(String s1, String s2){
  this.s1=s1;
  this.s2=s2;  
  start();
  }

  public void run(){
  while(true){
  synchronized(s1){

  synchronized(s2){
  System.out.println(s1+s2);  
  }
  }
  }
  }
}
class MyThread2 extends Thread{
  String s1;
  String s2;
  MyThread2(String s1,String s2){

  this.s1=s1;
  this.s2=s2;
  start();
  }

  public void run(){
  while(true){
  synchronized(s2){
  
  synchronized(s1){
  System.out.println(s2+s1);
  }
  }
  }
  }
}

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>

Download this example 

Advertisements

Share on Google+Share on Google+

Deadlock

Posted on: April 17, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

Ads
 

Discuss: Deadlock  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0

Ads

 

Ads