-2

I got the idea about why deadlock happens Deadlock example

and read the related questionsenter link description here

But, I modified the sample code by adding Thread.sleep(1000) between two start() call and this program was not blocked by deadlock.

public class Deadlock {
    static class Friend {
        private final String name;
        public Friend(String name) {
            this.name = name;
        }
        public String getName() {
            return this.name;
        }
        public synchronized void bow(Friend bower) {
            System.out.format("%s: %s"
                            + "  has bowed to me!%n",
                    this.name, bower.getName());
            bower.bowBack(this);
        }
        public synchronized void bowBack(Friend bower) {
            System.out.format("%s: %s"
                            + " has bowed back to me!%n",
                    this.name, bower.getName());
        }
    }

    public static void main(String[] args) throws Exception {
        final Friend alphonse =
                new Friend("Alphonse");
        final Friend gaston =
                new Friend("Gaston");
        new Thread(new Runnable() {
            public void run() { alphonse.bow(gaston); }
        }).start();
        Thread.sleep(1000);
        new Thread(new Runnable() {
            public void run() { gaston.bow(alphonse); }
        }).start();
    }
}

I'd like to know why this happens.

Is there any chance to exit normally without deadlock?

Community
  • 1
  • 1
Jeff
  • 373
  • 2
  • 13

1 Answers1

1

The tutorial states pretty clearly that the deadlock is caused by "the possibility that two friends might bow to each other at the same time." In your case, that's extremely unlikely, because the first friend has a 1 second head-start.

More technically, if both threads are inside bow() at the same time, they will each wait for the other's lock to be released to execute bowBack(), resulting in deadlock. But if one thread executes well before the other, it'll generally be able to execute bow() and bowBack() before the other thread obtains any lock.

shmosel
  • 49,289
  • 6
  • 73
  • 138
  • But, I tried many many times and it is always blocked by deadlock. Is there any chance to exit normally without deadlock? – Jeff Jan 19 '17 at 02:01
  • @Jeff You mean without `sleep()`? Sure, there's a chance, but there's no way to control it. `sleep()` is a good way to simulate the same result. – shmosel Jan 19 '17 at 02:02
  • Thanks for you reply. If there is a chance, then why does deadlock occurs with such high rate? – Jeff Jan 19 '17 at 02:07
  • @Jeff The actual likelihood depends on many things - your environment, code, number of threads, luck etc. But if you start two identical threads at (approximately) the same time, there's a good chance they'll be inside the same method at the same time. – shmosel Jan 19 '17 at 02:12
  • I see, I was struggling with this question through last whole night. now I got the idea. Thank you very much.:) – Jeff Jan 19 '17 at 02:16