I need to stall the main thread until a particular situation happens, so then I can start a different thread.
To freeze main thread I thought of launching another thread that checked for that particular situation periodically. When this situation is reached, then we can launch the second thread.
I knew I could get the "return value" of a thread using Future
and Callable
in this fashion and I also knew how to schedule threads.
But how can I mix both?
This is what I am trying to do:
Thread startResolution = new Thread(() -> target(path + "/" + id + "/schedule").request().get());
Thread stopResolution = new Thread(() -> {
Response response = target(path + "/" + id + "/schedule/stop-resolution").request().get();
System.out.println(response.readEntity(String.class));
});
startResolution.start();
// I want to lock the main thread here until this returs a particular state
target(path + "/" + id + "/schedule/resolution-state").request().get(String.class);
stopResolution.start();
startResolution.join();
stopResolution.join();
How could I do it using the tools I mentioned? Or maybe a CyclicBarrier
would be a better fit for this scenario?
The fact that HTTP requests are mixed in this situation makes it hard for me to figure out an approach.