1

Hello pleas help me with wait() and notifyall() I need it, cause of the do while repeat!, the function savegame() call over ion a server and after the function get an answer from the server, the other function should repeat.

The part is in one function which shoul wait.

public static Boolean notifier = Boolean.valueOf(false);
synchronized (notifier) {
       try {
           System.out.println("Spiel noch nicht gespeichert, warten...");
           notifier.wait();
       } catch (Exception f) {
           Log.e("Fehler", "Beim Warten");
       }
}

and this is the part in the function where something happen, after that the function above should get notify.

synchronized (notifier) {
   System.out.println("UNLOCK:" + System.currentTimeMillis());
   notifier.notifyAll();
}
JoJa
  • 23
  • 5
  • I'm sorry but this code is far too long and far too hard to read for me. Would it not be possible for you to simplify it and post a small snippet that can be compiled and produces the problem? – biziclop Sep 30 '14 at 16:04
  • ok now it is just the major part – JoJa Sep 30 '14 at 16:11
  • What is the observed behavior in cobtrast to what you expect? – Fildor Sep 30 '14 at 16:31
  • I have a Function that contacts a server but the functions calls under this call are executed before this is finished – JoJa Sep 30 '14 at 16:38
  • So the `wait()` call does not block the thread as expected, correct? – Fildor Sep 30 '14 at 16:43
  • It block the whole process and i could not open it again with notifyall(); – JoJa Sep 30 '14 at 16:48
  • Now I am completely confused. But I recommend using some helper from the concurrent namespace. Maybe a cyclicBarrier for the repeating task ... – Fildor Sep 30 '14 at 18:21
  • Even checking the edit history on this, I don't see any background threads being used. `wait()` and `notify()` are for threading. How are the threads started and synchronized? – Ian McLaird Sep 30 '14 at 18:25
  • 2
    Me too looking at the history: You assign `notifier` a different instance. So wait()/notify() will not work because you wait on one instance, while notifying another. Use two different Objects to transfer state information and syncing. Make the sync object static **and** final, so you can be sure, you are dealing with one instance only. __Or__ use a completely different approach :) – Fildor Sep 30 '14 at 19:22
  • I have one more question: http://stackoverflow.com/questions/26135930/show-notification-just-if-app-ist-closed-else-execute-a-function – JoJa Oct 01 '14 at 13:14

1 Answers1

1

Me too looking at the history: You assign notifier a different instance. So wait()/notify() will not work because you wait on one instance, while notifying another. Use two different Objects to transfer state information and syncing. Make the sync object static and final, so you can be sure, you are dealing with one instance only. Or use a completely different approach :)

Comment as answer, so it can be marked solved.

Fildor
  • 14,510
  • 4
  • 35
  • 67