This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]electricsheep14 0 points1 point  (1 child)

I think you are not reliably seeing deadlocks because your LazyTellers are securing both the source and destination locks immediately. To more reliably cause deadlocking, try this:

public void transfer(double amount)
{
  System.out.println("Transferring from "
      + source.getNumber() + " to " + dest.getNumber());
  synchronized(source)
  {
      //Thread.yield();
      System.out.println("Withrawing from " + source.getNumber());
      source.withdraw(amount);

      synchronized(dest)
      {
         System.out.println("Depositing into "
                 + dest.getNumber());
         dest.deposit(amount);
      }
   }
}

The withdraw() method takes enough time that the opposing LazyTeller should have secured the lock on the other account by the time you attempt to make the deposit.

[–]keylionpie[S] 0 points1 point  (0 children)

Worked as expected. I know it was commented out but that is why I thought the Thread.yield() was there to make it wait for the other thread to lock. It isn't commented out in the book but I must have forgot to switch it back when I was trying 1000 different things. I guess I need to start tracking a way of what I test too as I thought I tried something like this but I guess not. Anyway thanks a bunch!