• Post Reply Bookmark Topic Watch Topic
  • New Topic

dead-locking scenario

 
stan li
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code aims to simulate deadlocking scenario but the code runs smoothly without deadlock. What's wrong with my code? Thank you.

// SynTestDemo4.java
// dead-locking simulation

class SynTestDemo4 extends Thread
{
public static void main (String [] args)
{

TransThread1 tt = new TransThread1 ();

new Thread(tt).start();

try{ Thread.sleep (1); } catch (InterruptedException e){}

tt.str = new String ("Method");

new Thread(tt).start();

}

}



class TransThread1 implements Runnable
{

int amount = 100;
int increament = 10;
String str = new String("");

public void run()
{

if (str.equals("Method"))
{
amountadd();
}

else

{
synchronized(str)
{

amount = amount+increament;

try{ Thread.sleep (10); } catch (InterruptedException e){}
synchronized(this){}

System.out.println(Thread.currentThread().getName()+" set amount to "+amount);

}
}

}




public synchronized void amountadd()
{
{
amount = amount+increament;

try{ Thread.sleep (10); } catch (InterruptedException e){}
synchronized(str){}

System.out.println(Thread.currentThread().getName()+" set amount to "+amount);
System.out.println("id is " +str);

}
}


}
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, stan!

We've a whole forum dedicated to multithreaded issues.

I'm moving this to the Threads and Synchronization forum...
 
Jeremy Thornton
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi stan,

Deadlock occurs when two (or more) threads hold a lock that the others need to complete.
e.g.
Thread A holds a lock on object 1 and needs a lock on object 2 to complete.
Thread B holds a lock on object 2 and needs a lock on object 1 to complete.

Your code starts two Threads (incidentaly the name TransThread1 is a bit misleading as its not a Thread) which use the same Runnable object.
For the first Thread the str member of the Runnable object *may* be set to "Method" when it gets to the place in the run() method that uses it.
For the second Thread the str memeber of the Runnable object *will* be set to "Method".

As your using separate Threads, each could run before, after or concurrently with the other.

You're getting a lock on an object which changes at run time - probably not what you intended i.e. the objects created for "" and for "Method" have different locks and if str member has been changed the second thread will always successfully get the lock regardless of whether the original thread holds the lock for a different object.

In addition, you always get the locks in the same order. Even if the same object is synchronized on, the second thread will just wait until the first releases the lock and then go about its business.

In short, if you want to see deadlocking in all its glory you'll need to change your runnable class to do something like



Hope this makes sense,
Jeremy.
[ May 25, 2004: Message edited by: Jeremy Thornton ]
 
stan li
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am studying your code.
This is really a nice forum.
Thank you.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!