0

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.

Community
  • 1
  • 1
dabadaba
  • 9,064
  • 21
  • 85
  • 155
  • Why not just do the work _on the main thread_? When your condition is met, then spin up the other threads. – Boris the Spider Jul 04 '16 at 15:57
  • When you create another thread and then block the main thread, you aren't doing multithreading - so why pretend? – Boris the Spider Jul 04 '16 at 16:00
  • I am doing multithreading. The threads `startResolution` and `stopResolution` will run in paralell. Only that `stopResolution` needs to wait for a particular condition to happen to be launched. – dabadaba Jul 04 '16 at 16:02
  • 1
    Use Executors and Callables... they are the taylor made solution for you.. – ΦXocę 웃 Пepeúpa ツ Jul 04 '16 at 16:02
  • @ΦXocę웃Пepeúpa In what fashion should I use them? I can't figure out how to put everything together, that's what the question is about. It's like you're telling me that to play an instrument I need a guitar, but have in mind that (metaphorically) I just don't know that I am supposed to graze the strings. – dabadaba Jul 04 '16 at 16:27
  • 1) Create a `CompletableFuture`. 2) Call `thenRun` or `thenRunAsync` (depending on need) set up starting you other task. 3) call `complete` from the `startResolution` when the condition is met. 4) Throw this all away, rewrite the whole lot into 3 lines of code using the Java 8 concurrency API. 5) Pinch yourself whenever you start thinking in threads rather than higher order concurrency constructs. 6) Profit. – Boris the Spider Jul 04 '16 at 16:49
  • @BoristheSpider thank you for that, but I just can't see it. I have spent the entire afternoon for this and I can't put it together. I guess I'll abandon it (unless an answer is provided) since it's just to test 5 damn lines of code I know they already work, but I wanted to do things properly. – dabadaba Jul 04 '16 at 17:02

1 Answers1

0

Create and start the thread that checks on the required state and exits when the state happens, then join() it. The main thread will block on the join until the state occurs.

Alternatively, put the code checking for the required state at the beginning of the stopResolution thread and start it right away; then that thread will wait for the required state before proceeding with the logic that is currently in it. In this case the main thread doesn't have to change at all.

Warren Dew
  • 8,790
  • 3
  • 30
  • 44