I got an answer from a java discord, what I need here is a CountDownLatch. Here's how it would work in my code:
public class MRE {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
InputHandler[] handler = new InputHandler[1];
Thread thread = new Thread(() -> {
boolean[] matches = new boolean[1];
CountDownLatch latch = new CountDownLatch(1);
handler[0] = (message) -> {
matches[0] = message.equals("true");
latch.countDown();
};
try {
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
thread.start();
while (scanner.hasNext()) {
handler[0].handleMessage(scanner.nextLine());
}
}
interface InputHandler {
void handleMessage(String message);
}
}