Thread synchronization issue (in java)

My Java application has a download task that requires two server calls that can be parallelized. So I run Thread t1 (execution of task1 ) and Thread t2 (for task2 ). Then I want to complete a specific task3 task when both other tasks are completed (1 and 2). Naturally, I cannot determine which of task1 and task2 will be completed first ...

What would be the easiest (and safest) way to code this for you?

thanks for the help

+4
source share
4 answers

You have several options:

  • If task3 is in a separate thread, and tasks1 and tasks2 threads are exclusive to their tasks (there is no thread pool) and end when the task ends, you can use {T1.join (); T2.join ();} to wait for both threads. Pros: Easy. Cons: The situation is rarely simple.
  • If task3 is in a separate thread, you can use java.util.concurrent.CountDownLatch, common to all threads. Task 3 will wait for the latch, while task1 and task2 will reduce it. Pros: quite easy, not paying attention to the environment. Cons: T3 is required to be created before it really is needed.
  • If task3 should be created only after task 1 and task2 are completed (there is no separate thread for it until task 1 and task2 are completed), you will need to create something more complex. I would recommend either creating your own ExecutorService, which will add the condition n to the future, but only fulfill the future when the condition changes or create a management service that will check the conditions and present these futures based on these conditions. Reason, this is from the head, there may be simpler solutions. Pros: Resource Friendly. Cons: complicated.
+7
source

You can join both t1 and t2 (in any order), then run task3 after connecting.

+2
source

Embed the listener interface in your task3 and register it for both task1 and task2. Task 1 and Task2 will have to call their listener before ending. That way you can write to task3, whose task has already been completed, and when both are done, you can complete your third task.

Of course, if you can exit your task 1/2 with an exception, do not forget to set task3 as your UncaughtExceptionHandler

0
source

I do not want to interrupt 2 workflows (task1 and task2) when they finish their work, then you can wait for the conditions in task3 until the other two are finished. Something like that:

 public class Master { private Object monitor_ = new Object(); private boolean task1Finished_; private boolean task2Finished_; class Task1 extends Thread { public void run() { // do stuff synchronized (monitor_) { task1Finished_ = true; monitor_.notifyAll(); } // keep on working } } class Task2 extends Thread { public void run() { // do stuff synchronized (monitor_) { task1Finished_ = true; monitor_.notifyAll(); } // keep on working } } class Task3 extends Thread { public void run() { synchronized (monitor_) { while (!task1Finished_ || !task2Finished_) { try { monitor_.wait(); } catch (InterruptedException ignored) { } } } // do stuff } } // start tasks1, task2 and task3... } 
0
source

Source: https://habr.com/ru/post/1277555/


All Articles