I have one task that I start off immediately, various executors that run, but then I have a final task that must not run until the the first task has finished. I am some knowledge of concurrency, but things change so much I'm unsure what is the now the best/simplest approach ?
Asked
Active
Viewed 93 times
0
-
Take a look to the keywords future/success – nano_nano Aug 01 '13 at 15:59
-
It sounds like you want Guava's ListenableFuture – Ben Manes Aug 02 '13 at 05:47
-
Had a look, but no I dont want task 2 to run as soon as task 1 has finished, I want it to later, but only run if task1 has finished, sounds like COuntdownLatch will do. – Paul Taylor Aug 02 '13 at 08:13
2 Answers
1
In your case there are a few simple solutions:
- to have the first task call the second task
- to have a shared "trigger", for example a CountdownLatch, to enable task1 to communicate to task2 that it has completed its job
- use a single threaded executor and submit your tasks in the right order

assylias
- 321,522
- 82
- 660
- 783
-
Sorry forgot to say the second task does not want to start as soon as task 1 finishes, it needs to run as the last thing, but must not start unless task 1 has finished, no other tasks are dependent on task 1 – Paul Taylor Aug 01 '13 at 16:07
-
2Then you must know how many those other tasks there are, and initialize the `CountDownLatch` to that number + 1. Each task must countDown the latch. – Marko Topolnik Aug 01 '13 at 16:31
0
CountdownLatch or a Phaser (if the number of preceding tasks can change).

Rob Garwood
- 108
- 3
-
I know CountDownLatch but because only two tasks involved thought might be simpler version, never seen Phaser before thanks. – Paul Taylor Aug 01 '13 at 16:08
-
I ran first tasks in executor, and passed countdownlatch to it so it so it could call countdown(), then called await() before starting second task so that it waits – Paul Taylor Aug 02 '13 at 10:37