0

I want to make something inside a thread that return what he did as string, and I'd like to wait for that string to do other thins. I've been reading about wait() and notify() but I dint get it. Can anyone help me?

Here I create the thread that does the operations

new Thread(

new Runnable() {

    @Override
    public void run() {

        synchronized(mensaje) {

            try {
                mensaje.wait();
                mensaje = getFilesFromUrl(value);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

}).start();

And here where I wait for the string mensaje changes

If the string is not "" then I show a button and some text

synchronized(mensaje) {

    if (mensaje.equals("")) {

        try {
            mensaje.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    btnOk.setVisibility(View.VISIBLE);
    lblEstado.setText(mensaje);
}

All this stuff is inside a method

Nikhil Agrawal
  • 26,128
  • 21
  • 90
  • 126
mesacuadrada
  • 386
  • 1
  • 4
  • 12

1 Answers1

1

notify(), notifyAll() and wait() basically work like this:

When you call wait() it releases the mutex that was taken by the synchronized block and puts the current thread to sleep in a queue.

notify() grabs one waiting thread from the front of the queue. That thread reacquires the mutex and continues running.

notifyAll() wakes up all threads in the queue.

To use this here is some pseudocode (lacks exception handling etc to be a bit more clear):

// in the thread that is supposed to wait
synchronized {
    while(!someCondition) {
        wait();
    }
    // At this point the other thread has made the condition true and notified you.
}


// In the other thread
synchronized {
    // Do something that changes someCondition to true.
    notifyAll();
}

Edit: Or as Thilo wrote look at java.util.concurrent first. There might already be a ready-made solution for your use case. No need to use low-level constructs then.

Correction: There is a ready-made solution for your use-case: http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html

and a corresponding Executor.

confusopoly
  • 1,245
  • 7
  • 19
  • Ok, it seems very easy, but didn't work, just does not do anything. I just want to disable some buttons and enable them after the thread activity... but it's getting complicated! – mesacuadrada May 18 '13 at 12:02
  • One problem I see in your code is that both threads call wait(). Do both threads use notify()? Otherwise this leads to both threads blocking and waiting for a notify() that never comes. Edit: I just noticed your comment above. Did you call notify() from inside the synchronized block? It doesn't work if you don't hold the monitor when you call notify. – confusopoly May 18 '13 at 12:07
  • that was a mistake that dind't realized, but i can't enter the new code... i call wait() from main thread and notify() from the new thread... – mesacuadrada May 18 '13 at 12:13
  • Have a look at http://stackoverflow.com/questions/886722/how-to-use-wait-and-notify-in-java – Tofeeq Ahmad May 18 '13 at 12:36