user1338319
user1338319

Reputation: 1

Please Explain Deadlock Concept

I was going through Java threads, specially the deadlock concept, I have found this below code:

public static void main(String... a) {

    final String o1 = "Lock ";
    final String o2 = "Step ";

    Thread th1 = new Thread() {
        public void run() {
            while (true) {
                synchronized (o1) {
                    synchronized (o2) {
                        System.out.println(o1 + o2);
                    }
                }
            }
        }

    };

    Thread th2 = new Thread() {
        public void run() {
            while (true) {
                synchronized (o2) {
                    synchronized (o1) {
                        System.out.println(o2 + o1);
                    }
                }
            }
        }

    };

    new Thread(th1).start();
    new Thread(th2).start();

}

Please explain what the program is doing as per my understandings a lock has been taken by one thread and trying to take another lock and the same is done by other thread , and when finally when we start two threads both got stuck up, Is there any other way to create deadlock of the above program, please advise and also the lock which is being taken in the above code is it the instance level lock.

Upvotes: 0

Views: 439

Answers (3)

assylias
assylias

Reputation: 328568

Based on your edited post, th1 could acquire o1 while th2 could acquire o2. Then both threads are waiting for the other to release the lock they have not acquired yet, and it never happens ==> deadlock.

Upvotes: 0

NPE
NPE

Reputation: 500167

Consider the following scenario:

  • th1 locks o1 and is interrupted before it gets a chance to lock o2;
  • th2 locks o2 and tries to lock o1.

Neither thread can make further progress, and you have a deadlock.

The original version of your code (before the edit) had no possibility of deadlock, since both threads acquired the two locks in the same order (o1 then o2).

Upvotes: 2

Sam DeHaan
Sam DeHaan

Reputation: 10325

To create Deadlock, you need to create a situation where multiple threads are waiting for locks held by other threads. For example:

  • Thread 1 needs Lock1 and Lock2
  • Requests in order: Lock1, Lock2
  • Thread 2 needs Lock1 and Lock2
  • Requests in order: Lock2, Lock1

    1. Thread 1 requests Lock1.
    2. Thread 2 requests Lock2.
    3. Thread 1 gets Lock1.
    4. Thread 2 gets Lock2.
    5. Thread 1 requests Lock2.
    6. Thread 2 requests Lock1.
    7. Thread 1 cannot have Lock2, Thread 2 is holding it. /Thread 1 waits...
    8. Thread 2 cannot have Lock1, Thread 1 is holding it. /Thread 2 waits...

In your (first, unedited) example, both threads request the locks they need in the same order and this is very important. If Thread1 gets Lock1, Thread2 cannot get Lock2 and create Deadlock, because it is still waiting for Lock1. Deadlock is avoided, because both threads attempt to acquire locks in the same order.

In your (new, edited) example, Deadlock can happen as explained above.

Upvotes: 0

Related Questions