How do we use the wait() method in LWJGL 2.9.1

Started by DarkerMinecraft, April 03, 2019, 14:56:01

Previous topic - Next topic

DarkerMinecraft

I have two threads for server and the rest of everything else. I need to wait in the main thread until something happens in the server thread so how do I use wait in the main thread and notifyAll in the server thread?

KaiHH

- https://www.baeldung.com/java-wait-notify
- https://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

// variables/fields visible to main thread and server thread:
volatile boolean shouldContinue;
Object sharedObject = new Object();

...

// In main thread (waiting):
synchronized (sharedObject) {
  while (!shouldContinue)
    sharedObject.wait();
}

...

// In server thread (notifying):
synchronized (sharedObject) {
  shouldContinue = true;
  sharedObject.notifyAll();
}