Jerry Jerry - 1 year ago 139
Java Question

How to Implement a DAG-like Scheduler in Java?

I want to implement a simple DAG-like scheduler in Java (no result needed), described as the following graph:

DAG-like scheduling

I can simply use manual code to achieve this:

ExecutorService executor = Executors.newCachedThreadPool();
Future<?> futureA = executor.submit(new Task("A"));
Future<?> futureC = executor.submit(new Task("C"));
Future<?> futureB = executor.submit(new Task("B"));
Future<?> futureD = executor.submit(new Task("D"));

But I'm looking for a more general way to do this, so I can use the scheduler like this:

Container container = new Container();
container.addTask("A", new Task("A"));
container.addTask("B", new Task("B"), "A");
container.addTask("C", new Task("C"));
container.addTask("D", new Task("D"), "B", "C");

And actually I've already implement a simple one:

But I need to iterate all the tasks every 100ms to see which one is ready to be submitted. Also in this implementation there's no exception checking.

I also checkout the Guava lib's ListenableFuture, but I don't know how to use it properly.

Any suggestions on how to implement a DAG, or recommending an existing opensource scheduler will be appreciated.

Answer Source

What you're looking for can be done using the google's guava library and it's listenable future interface. ListenableFutures allow you to have complex chains of asynchronous operations. You should implement a listenable future to execute task D once task B and C are completed using the allAsList method.

The documentation for Listenable Futures:

A tutorial on Listenable Futures:

An example of using the allAsList, chain, and transform methods:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download