Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use notify and wait

Can wait/notify be used within one thread? I'm mean I have a listener and in the moment when that listener gets called I wanna enable a thread to do his work.How could I do that?

UPDATE:My data is written in a database...and is written each time the listener is called.Now the thread that I've created reads that data and sends it somewhere.... Next...I get some other data and do the same thing....The other thread needs to know what was the last data he read it so he can start reading from where he left....

Take a look in here: using wait and notify within one thread This is how my problem looks like.Thx

I have the following:

synchronized (syncToken)
{
    try {
        syncToken.wait();

    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
System.out.println("MyThread: " + s);

in MyThread....so when I do

MyThread t = new MyThread(syncToken);
t.start();

I put my thread on waiting...yes?

And when I do this:

syncToken.notify();

I get my thread back on track....but the execution of the next line is the one after wait()?

I mean this: System.out.println("MyThread: " + s); ????

When u notify a thred does he continues his execution with the line after wait()???Thx

like image 318
adrian Avatar asked May 06 '11 19:05

adrian


People also ask

When to Use wait and notify?

Differences between wait() and notify()When wait() is called on a thread holding the monitor lock, it surrenders the monitor lock and enters the waiting state. When the notify() is called on a thread holding the monitor lock, it symbolizes that the thread is soon going to surrender the lock.

How do wait () notify () and notifyAll () work?

The wait() method causes the current thread to wait until another thread invokes the notify() or notifyAll() methods for that object. The notify() method wakes up a single thread that is waiting on that object's monitor. The notifyAll() method wakes up all threads that are waiting on that object's monitor.

How do you use notify method?

notify() wakes up a single thread that is waiting on this object's monitor. If many threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation. A thread waits on an object's monitor by calling one of the wait methods.


2 Answers

The following is a simple example of concurrency between two different threads. In the example the main thread start a MyThread thread and every 3 seconds it sets a data to the MyThread instance and then MyThread prints it. The idea is to have a synchronized object that you wait on it and notify in the end of the usage to other threads that they can use it:

Test.java:

package stack;

public class Test {
    public static void main (String args[])
    {
        Object syncToken = new Object();
        MyThread t = new MyThread(syncToken);
        t.start();
        for (int i = 0; i < 10; i++)
        {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized(syncToken)
            {
                t.setText("Iteration " + i);
                syncToken.notify();
            }
        }
    }
}

MyThread.java:

package stack;

public class MyThread extends Thread{

    String s;
    Object syncToken;
    public MyThread(Object syncToken)
    {
        this.s = "";
        this.syncToken = syncToken;
    }

    public void run()
    {
        while(true) // you will need to set some condition if you want to stop the thread in a certain time...
        {
            synchronized (syncToken)
            {
                try {
                    syncToken.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("MyThread: " + s);
        }
    }

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

In this example, the main thread sets a string (every 3 seconds) and the MyThread thread prints it.

Adapt it to your needs, it shouldn't be too hard.

like image 196
MByD Avatar answered Sep 28 '22 07:09

MByD


I had similar problem. I created an arbiter used by two threads (in your case it can be listeners thread and your task thread): listener:

arbiter.waitConsumer();
// prepare data
arbiter.dataLoaded();

task thread:

while(true){
  arbiter.waitProducer();
  // consume data
  arbiter.dataConsumed();
}

arbiter:

public class Arbiter {
private boolean dataLoaded = false;
public synchronized void waitProducer(){
    while(!dataLoaded){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public synchronized void waitConsumer(){
    while(dataLoaded){
        try {
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
public synchronized void dataLoaded(){
    dataLoaded = true;
    notify();
}public synchronized void dataConsumed(){
    dataLoaded = false;
    notify();
}}

Listener and task will synchronize themselfes against arbiters monitor. Probably you can call your arbiter queue or pipe and store date for consuming in it?

like image 30
igor.beslic Avatar answered Sep 28 '22 06:09

igor.beslic