LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: DarkerMinecraft on April 03, 2019, 14:56:01

Title: How do we use the wait() method in LWJGL 2.9.1
Post by: DarkerMinecraft on April 03, 2019, 14:56:01
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?
Title: Re: How do we use the wait() method in LWJGL 2.9.1
Post by: KaiHH on April 03, 2019, 21:37:29
- 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();
}