79146573

Date: 2024-10-31 23:26:29
Score: 1
Natty:
Report link

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);
    }
}
Reasons:
  • Blacklisted phrase (0.5): I need
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: blockgoblin31