Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can two threads waiting on the same monitor be called a deadlock?

Two threads are waiting on the same monitor, for example if one thread calls wait on 'lock' and the other thread that acquired the monitor also calls wait before notifying the first thread. Now both the threads are waiting but no one gets notified. What would I call this situation? Can this be called a deadlock?

Edit: Assumption is that these are the only two threads and there is no way for them to be notified from elsewhere.
Update: I just created the situation I described in question. The following piece of code works okay most of the time when changer thread is started before the listener thread. However when I start listener before changer, the program just hangs after printing two lines (one from changer and one from listener thread). Would the situation where I call listener before changer be called a deadlock?

package demo;

public class ProducerConsumer {

public static int SAMPLE_INT = 0;

public static void main(String[] args) {

    PC pc = new PC();

     Thread changer = new Thread(new Runnable() {
        public void run(){
            try {
                pc.producer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
     });

    Thread listener = new Thread(new Runnable(){
        public void run() {
            try {
                pc.consumer();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    changer.start();
    listener.start(); 
   }
 }

class PC {

Object lock = new Object();

public void producer() throws InterruptedException {
    synchronized(this){
        for (int i=0; i<5; i++){
            ProducerConsumer.SAMPLE_INT++;
            System.out.println("Changed value of int to: " + ProducerConsumer.SAMPLE_INT);
            wait();
            notify();
           }
    }
}

public void consumer() throws InterruptedException{
    synchronized(this){
        for (int i=0; i<5; i++){
            System.out.println("Receieved Change: " + ProducerConsumer.SAMPLE_INT);
            notify();
            wait();
           }
         }
       }
     }


Output when changer is started before listener:
Changed value of int to: 1
Receieved Change: 1
Changed value of int to: 2
Receieved Change: 2
Changed value of int to: 3
Receieved Change: 3
Changed value of int to: 4
Receieved Change: 4
Changed value of int to: 5
Receieved Change: 5
Program terminates.

Output when listener is started before changer:
Receieved Change: 0
Changed value of int to: 1
Program doesn't terminate.

Thanks.

like image 365
S. Doe Avatar asked Feb 17 '16 06:02

S. Doe


People also ask

Can deadlock occur with two threads?

A process with two or more threads can deadlock when the following conditions hold: Threads that are already holding locks request new locks. The requests for new locks are made concurrently. Two or more threads form a circular chain in which each thread waits for a lock which is held by the next thread in the chain.

Can deadlock occur with monitors?

If we insist that the awakened thread be the first to run in the inner monitor after the signal, then deadlock will result. One way to avoid this problem is to arrange for mutual exclusion across all the monitors of a program.

Can threads cause deadlock?

A deadlock occurs when two threads each lock a different variable at the same time and then try to lock the variable that the other thread already locked. As a result, each thread stops executing and waits for the other thread to release the variable.

What is a deadlock in threads?

Deadlock describes a situation where two or more threads are blocked forever, waiting for each other. Deadlock occurs when multiple threads need the same locks but obtain them in different order.


1 Answers

If these are the only two threads involved (eg with access to the monitor) then yes. If there are additional threads which can access the monitor and unlock it then no.

Keep in mind however that you are talking about two topics - a monitor is generally a mutex in threading terms. But wait is something associated with a condition variable which while requiring a mutex to work, performs a more subtle task of deliberately blocking on a thread based on a condition which one thread signals to another with a caveat known as spurious wakeups.

like image 73
Andy Avatar answered Sep 28 '22 03:09

Andy