-
Notifications
You must be signed in to change notification settings - Fork 6
About Graph Execution
So what exactly is graph execution? Since graph execution is the core of the JavaRed library, first, let's discuss the what and why.
We will define 3 different levels of concurrent executions:
- Linear Execution.
- Concurrent Execution.
- Graph Execution.
A linear execution flow is an execution flow, executing one or more tasks, one at a time. With the completion of each task, the flow moves on to the next task and so on.
Example:
Blocking implementation:
preformTaskA();
preformTaskB();
preformTaskC();
preformTaskD();
Non-blocking implementation:
preformTaskA().whenFinished(() -> {
preformTaskB().whenFinished(() -> {
preformTaskC().whenFinished(() -> {
preformTaskD();
});
});
});
A concurrent execution means that we can execute multiple tasks at the same time.
Example:
Blocking implementation:
Future<?> aFuture = preformTaskA();
aFuture.get();
Future<?> bFuture = preformTaskB();
Future<?> cFuture = preformTaskC();
bFuture.get();
cFuture.get();
Future<?> dFuture = preformTaskD();
dFuture.get();
Future<?> eFuture = preformTaskE();
Non-blocking implementation:
preformTaskA().whenFinished(() -> {
CountDownLatch latch = new CountDownLatch(2);
Runnable onFinish = () -> {
latch.countDown();
if (latch.getCount() == 0) {
preformTaskD().whenFinished(() -> {
preformTaskE();
});
}
};
preformTaskB().whenFinished(() -> {
onFinish.run();
});
preformTaskC().whenFinished(() -> {
onFinish.run();
});
});
Graph execution is an execution flow which supports executing concurrent tasks, where every set of tasks can be scheduled to execute when every other set of tasks are finished. Simply put, every task, or group of tasks, can have one or more preconditions.
Example:
This type of execution cannot be implemented using blocking code. The non blocking example will be shown in the Graph module section.