Basically, Thread A wait()'s on a lock object, then Thread B notifyAll()'s. The function on thread A finishes, but doesn't return for normal operation.
It may be of note that I am using JavaFX. In order to keep my FX Thread separate from my work Threads, I make a new Thread in order to prevent issues when I lock it and such.
Here's my code (pared down to the functions in question):
public class ShortcutManager {
private static final StringProperty STATE = new SimpleStringProperty("Create Shortcut");
public static void create() {
Thread creationThread = new Thread(() -> {
// Do some work (sends logs to the logger)
String result = prompt();
if (result != null) {
// Do more work (sends logs to the logger)
} else {
// Do other work (sends logs to the logger)
}
}
creationThread.start();
}
private static String prompt() {
STATE.setValue("Waiting for user...");
String result = popup.showAndWait();
System.out.println("Result: \"" + result + "\"");
return result;
}
public class CmlPopup implements Initializable {
private boolean isClosed = false;
public synchronized String showAndWait() {
isClosed = false;
Platform.runLater(() -> {
stage.show();
});
while (!isClosed) {
try {
System.out.println("Waiting.");
wait();
System.out.println("Notified.");
} catch (InterruptedException ex) {
}
}
return getResult();
}
public synchronized void close() {
stage.close();
isClosed = true;
System.out.println("Closing and notifying");
notifyAll();
}
}
Now - why am I locking it myself when the Stage has a showAndWait() function? Because that wasn't working either, plus it ends up pausing the FX thread instead of the creation thread, since both show() and showAndWait() must occur on the FX thread.
My output is as follows:
. . .
ShortcutManager State: Waiting for user...
Waiting.
Closing and notifying.
Notified.
And then nothing more. No printing of the result; no other work is done by the thread. My FX thread and other work threads operate fine, but this one seems to just terminate even though there is still more to be done.
P.S. Before you criticize me, I'm using Loggers where I intend to retain them, and System.out when I intend to remove them.