0

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 ?

Paul Taylor
  • 13,411
  • 42
  • 184
  • 351

2 Answers2

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
  • 2
    Then 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