Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Deadlock 
 

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{
  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 

                         

» View all related tutorials
Related Tags: java c class interface object io read thread int this creation extend run lan ext e il section can im

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.