Notify () and notifyAll () not working in my java code

So, I was working on a simple wait / notify example in Java and for some reason I was not able to get it to work correctly. If someone can understand what might be the problem, it would be very helpful!

class producer implements Runnable {
    StringBuffer sb;
    producer() {
        sb=new StringBuffer("");
    }

    public void run () {
        synchronized(sb) {
            for(int i = 0; i < 10; i++) {
                try { 
                    sb.append(i+" ");
                    System.out.println("Appending ... "); 
                } catch (Exception e) {}
            }
            sb.notify(); 
        }
    }
}

class consumer implements Runnable {
    producer p;
    consumer(producer pp) {
        this.p = pp;
    }

    public void run() {
        System.out.println("Rreached");
        synchronized(p.sb) { 
            try { 
                 p.sb.wait(); 
            } catch (Exception e) {}
            System.out.println(p.sb);
        }
    }
}

class Thread_Comunication {
    public static void main (String [] args) {
        producer p = new producer();
        consumer c = new consumer(p);

        Thread t1 = new Thread(p);
        Thread t2 = new Thread(c);
        t1.start();
        t2.start();
    }
}

Conclusion:

Appending ...
Rreached   // randome Position
Appending ...
Appending ...
Appending ...
Appending ...
Appending ...
Appending ...
Appending ...
Appending ...
Appending ...  

So, for some reason, the thread is t1not waking up t2or am I missing something at all?

+4
source share
2 answers

Notify does nothing unless another thread is waiting. Your code depends solely on the notification (where it needs the condition variable) and relies on the user working with the manufacturer to work.

, ; , . ( , , sb, .) , , . , .

, .

, , , , . , , . , , , , .

:

import java.io.*;

class producer implements Runnable {
    StringBuffer sb;
    boolean done = false;
    producer() {
        sb=new StringBuffer("");
    }

    public void run () {

        synchronized(sb) {

            for(int i=0;i<10;i++) {
                try { 
                    sb.append(i+" ");
                    System.out.println("Appending ... "); 
                } catch (Exception e) {}
            }
            sb.notify(); 
            done = true;
        }


    }
}

class consumer implements Runnable {
    producer p;
    consumer(producer pp) {
        this.p=pp;
    }

    public void run() {
        System.out.println("Rreached");
        synchronized(p.sb) { 
            try { 
                while (!p.done) {
                 p.sb.wait(); 
                }
            } catch (Exception e) {}

            System.out.println(p.sb);
        }
    }
}


public class Communication {
    public static void main (String [] args) throws Exception {
        producer p= new producer();
        consumer c= new consumer(p);

        Thread t1= new Thread(p);
        Thread t2= new Thread(c);
        t2.start();
        t1.start();
    }
}
+3

, wait/notify Java - .

, notify() . producer synchronized, consumer wait, producer . notify synchronized, , consumer wait(), producer notify. , consumer .

consumer, , , producer synchronized - , consumer a System.out.println() . "", producer Thread.sleep(100) synchronized, , , consumer wait() producer .

. wait/notify , consumer. sb, . , consumer - :

synchronized (p.sb) { 
    try {
        // do we need to wait to see if the producer added stuff?
        if (p.sb.length() == 0) {
            p.sb.wait();
        }
    } catch (InterruptedException e) {
        // this is always a good pattern to preserve the interrupt flag
        Thread.currentThread().interrupt();
        return;
    }
}
+1

Source: https://habr.com/ru/post/1651788/


All Articles