Hello Guest

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

  • 1 Replies
  • 171 Views
How do we use the wait() method in LWJGL 2.9.1
« 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?

*

Offline KaiHH

  • ***
  • 181
Re: How do we use the wait() method in LWJGL 2.9.1
« Reply #1 on: April 03, 2019, 21:37:29 »
- https://www.baeldung.com/java-wait-notify
- https://docs.oracle.com/javase/tutorial/essential/concurrency/guardmeth.html

Code: [Select]
// 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();
}