简体   繁体   中英

wait() and notifyall() not working

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();
}

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.

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