user2082928
user2082928

Reputation: 11

Java Threads wait, notify in one class giving unexpected results

/I had written the below program to wait() and notify(). But after going to First put() method, the threads are not going forward. Can anybody explain me what is the error I had done in the below code./

public class ThreadJoin {
    public void buildThread() throws InterruptedException {
        Adder a = new Adder("Test");
        Thread t1 = new Thread(a, "First");
        Thread t2 = new Thread(a, "second");

        t1.start();
        t2.start();
    }

    public static void main(String[] args) throws InterruptedException {
        new ThreadJoin().buildThread();
    }
}

class Adder implements Runnable {
    String name;
    int i;
    private boolean suspendFlag = false;

    public Adder(String name) {
        this.name = name;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
            for(int i = 0; i < 10; i++){
                put(i);
            }

            for(int j = 0 ; j < 10; j++){
                get();
            }
    }

    public synchronized void put(int i) {
        this.i = i;

        try {
            while (suspendFlag) {
                wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("put: " + i+" currentThread: "+Thread.currentThread().getName());
        suspendFlag = true;
        notify();
    }

    public synchronized void get() {
        System.out.println("Entering into the get method");

        try {
            if (!suspendFlag) {
                wait();
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("get: " + i+" currentThread: "+Thread.currentThread().getName());
        suspendFlag = true;
        notify();
    }
}

Upvotes: 0

Views: 75

Answers (2)

Patricia Shanahan
Patricia Shanahan

Reputation: 26185

The simple view of what is going on is that you have a wait-notify structure that forces alternate put and get calls. You have two threads, each of which is going to do 10 put calls before its first get, so the alternation is not possible. Both threads end up in put waiting for the other to do a get.

You could change your design to allow multiple put calls without a get, or change the driving code so that one thread does put calls and the other does get calls.

Upvotes: 1

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 280122

Whichever thread executes first (say First) will execute the synchronized put method, locking the monitor of the Adder object. While this is happening, the other thread (second) cannot start executing the put method since it cannot lock the monitor of the same Adder object.

So First sees that suspendFlag is false and therefore skips the while. It makes supsedFlag true and calls notify(). There is currently no thread waiting on the Adder object, so the call to notify() does nothing.

When this thread ends execution of the put method, the monitor is released and second potentially gets it. If this happens, it sees that suspendFlag is true and enters the while block, executing wait(). Doing so, it releases the monitor. suspendFlag remains true.

Execution returns to First, which loops its for and reinvokes put. It sees that suspendFlag is true and enters the while loop, invoking wait().

Now both your threads are in wait and there is nothing to notify them. Your program stalls.

Upvotes: 2

Related Questions