简体   繁体   中英

wait()/notify() not working properly

I have a ConsumerProducer object on which I want to acquire lock from two different threads. The class is as below:

public class ConsumerProducer {

    public String stringPool = null;

    public void put(String s){
        stringPool = s;
    }

    public String get(){
        String ret = stringPool;
        stringPool = null;
        return ret;
    }

}

The thread impl class is as below:

public class WaitNotifyTest implements Runnable {

    private String threadType;
    public ConsumerProducer cp;
    public static volatile int i = 1;

    public WaitNotifyTest(String threadType, ConsumerProducer cp) {
        this.threadType = threadType;
        this.cp = cp;
    }

    public static void main(String[] args) throws InterruptedException {

        ConsumerProducer cp = new ConsumerProducer();
        WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp);
        WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp);

        Thread t1 = new Thread(test1);
        Thread t2 = new Thread(test2);

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

    @Override
    public void run() {
        while (true) {

            if (threadType.equalsIgnoreCase("Consumer")) {
                synchronized (cp) {
                    try {
                        if (null != cp.get()) {
                            cp.wait();
                        }
                        consume();
                        System.out.println("notify from Consumer");
                        cp.notify();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            } else {
                synchronized (cp) {
                    try {
                        if (null == cp.get()) {
                            cp.wait();
                        }
                        produce();
                        System.out.println("notify from Producer");
                        cp.notify();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (i == 5) {
                break;
            }
            i++;
        }
    }

    public void consume() {
        System.out.println("Putting: Counter" + i);
        cp.put("Counter" + i);
    }

    public void produce() {
        System.out.println("getting: " + cp.get());
    }

}

But when I run the code it is facing some kind of deadlock and it is stuck printing like

Putting: Counter3
notify from Consumer

Something is going terribly wrong but I am not able to identify. Please help.

Your consumer is doing producer's job and producer is doing consumer's job. Exchange their responsibility and modify the condition to wait. Please refer to the code below.

  1. Consumer will wait when there is nothing to get and he will release the lock of cp. So that producer has chance to go into the synchronized block.
  2. Producer only produces when there is nothing or he will wait. After that, he will release the lock of cp. So that consumer has chance to go into the synchronized block.
  3. Consumer is who get things away.
  4. Producer is who put things to table.
  5. According to your comment. You want to put Counter from 1 to 5, so you should add i++ only in Producer thread. How can you control its increase in both threads?
  6. You don't judge whether it's consumer or producer calling the get() from cp object but assign null to stringPool . It's obvious wrong and will make consumer get null from public space. I add a new method clearString() which will set public space to null only when consumer has comsumed the product.

     public class WaitNotifyTest implements Runnable { private String threadType; public ConsumerProducer cp; public static volatile int i = 0; public WaitNotifyTest(String threadType, ConsumerProducer cp) { this.threadType = threadType; this.cp = cp; } public static void main(String[] args) throws InterruptedException { ConsumerProducer cp = new ConsumerProducer(); WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp); WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp); Thread t1 = new Thread(test1); Thread t2 = new Thread(test2); t1.start(); t2.start(); t1.join(); t2.join(); } @Override public void run() { while (true) { if (threadType.equalsIgnoreCase("Consumer")) { synchronized (cp) { try { /* * Consumer will wait when there is nothing to get and he will release the lock of cp. * So that producer has change to go into the synchronized block. */ if (null == cp.get()) { cp.wait(); } consume(); System.out.println("notify from Consumer"); cp.notify(); } catch (InterruptedException e) { e.printStackTrace(); } } } else { synchronized (cp) { try { /* * Producer only produce when there is nothing or he will wait. At the same time, he will release the lock of cp. * So that consumer has chance to go into the synchronized block. */ if (null != cp.get()) { cp.wait(); } i++; produce(); System.out.println("notify from Producer"); cp.notify(); } catch (InterruptedException e) { e.printStackTrace(); } } } if (i == 5) { break; } } } public void consume() { System.out.println("getting: " + cp.get()); cp.clearString(); } public void produce() { System.out.println("Putting: Counter" + i); cp.put("Counter" + i); }} 

Also see the ConsumerProducer class.

public class ConsumerProducer {
        public String stringPool = null;

        public void put(String s){
            stringPool = s;
        }

        public String get(){
            return stringPool;
        }

        public void clearString(){
            stringPool = null;
        }
}

Updated code is here: ConsumerProducer.java:
public class ConsumerProducer {

    public volatile String stringPool = null;

    public void put(String s){
        this.stringPool = s;
    }

    public String get(){
        String ret = this.stringPool;
        //this.stringPool = null;
        return ret;
    }
    //added
    public void clearString(){
        this.stringPool = null;
    }

}

WaitNotifyTest.java public class WaitNotifyTest implements Runnable {

    private String threadType;
    public ConsumerProducer cp;
    public static volatile int i = 0;

    public WaitNotifyTest(String threadType, ConsumerProducer cp) {
        this.threadType = threadType;
        this.cp = cp;
    }

    public static void main(String[] args) throws InterruptedException {

        ConsumerProducer cp = new ConsumerProducer();
        WaitNotifyTest test1 = new WaitNotifyTest("Consumer", cp);
        WaitNotifyTest test2 = new WaitNotifyTest("Producer", cp);

        Thread t1 = new Thread(test1);
        Thread t2 = new Thread(test2);

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

    @Override
    public void run() {
        while (true) {

            if (threadType.equalsIgnoreCase("Consumer")) {
                synchronized (cp) {
                    try {
                        if (null == cp.get()) {
                            cp.wait();
                        }
                        consume();
                        System.out.println("notify from Consumer");
                        cp.notify();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            } else {
                synchronized (cp) {
                    try {
                        if (null != cp.get()) {
                            cp.wait();
                        }
                        i++;
                        produce();
                        System.out.println("notify from Producer");
                        cp.notify();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            if (i == 5) {
                break;
            }

        }
    }

    public void produce() {
        System.out.println("Putting: Counter" + i);
        cp.put("Counter" + i);
    }

    public void consume() {
        System.out.println("getting: " + cp.get());
        cp.clearString();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM