• Post Reply Bookmark Topic Watch Topic
  • New Topic

deadlock in java multithreading  RSS feed

 
vignesh gopalakrishnan
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my own coding for understanding deadlock in multithreading . But my doubt is whether this program mirrors the concept of deadlock perfectly or not.
If not what should i do to make this code a perfect deadlock.

import java.io.*;


class A
{
B bc;
synchronized void funcA(B b)
{
bc=b;
System.out.println("INSIDE FIRST OBJECTS MONITOR");

try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
bc.funcB(new A());
}

}


class B
{
A ac;
synchronized void funcB(A a)
{
ac=a;
System.out.println("INSIDE SECOND OBJECT MONITOR");

try
{
Thread.sleep(5000);
}
catch(InterruptedException e)
{
System.out.println(e);
}

ac.funcA(new B());
}

}


class deadlockA implements Runnable
{
Thread t;
A a1;
B b2;
deadlockA(B b1)
{
a1=new A();
b2=b1;
t=new Thread(this);
t.start();
}

public void run()
{
a1.funcA(b2);
}

}


class deadlockB implements Runnable
{
Thread t;
A a4;
B b3;

deadlockB(A a3)
{
b3=new B();
a4=a3;
t=new Thread(this);
t.start();
}

public void run()
{
b3.funcB(a4);
}

}


class deadlock
{
public static void main(String args[])
{

A A1=new A();
B B1=new B();

deadlockA da=new deadlockA(B1);
deadlockB db=new deadlockB(A1);

}
}
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hate to point out the obvious, but you really should consider formatting your code. Unformatted code is very difficult to read.

Henry
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Define "mirrors the concept of deadlock" and "perfect deadlock".
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Also... after looking at the code for a few minutes, I couldn't really find the deadlock. Does this code actually deadlock when you run it?


Regardless, even if it does deadlock, at best, it is a poor representation of a deadlock, as it is very difficult to envision what a deadlock is, since it is very difficult to find the deadlock (assuming that there is one) in the code.

Henry

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!