8

I have a bunch of runnables I want to run in multiple threads and some depend on others to complete before they begin. I wrote a simple utility to do this, but is there a library that already provides this capability?

jonderry
  • 23,013
  • 32
  • 104
  • 171

2 Answers2

2

You can use a CountDownLatch to coordinate the activites of threads

Cratylus
  • 52,998
  • 69
  • 209
  • 339
  • Useful answer, though it doesn't quite provide a simple api to run dependent tasks. – jonderry Aug 30 '12 at 22:31
  • This seems like a simple enough problem that rolling your own system wouldn't be too bad (and any other library is going to be overkill). I would implement it by having one class that acts as a "node" - it wraps a Runnable, and it knows its dependencies and dependents, and also has a `CountDownLatch` for coordination. Then another class to take in a bunch of these, calculate a total ordering over them, and feed them to an Executor in that order (to prevent deadlock). – Joe K Aug 30 '12 at 23:10
1

"some depend on others to complete before they begin".

I assume this means some tasks use results of other tasks as input arguments. If so, search for "java dataflow" or "java workflow".

If input arguments for each task can be represented with a single sequential queue, this special kind of dataflow is known as "Actor model", so search for "java actor library or framework".

In particular, an opensource project of mine df4j supports both dataflow and actor styles.

Alexei Kaigorodov
  • 13,189
  • 1
  • 21
  • 38