Skip to content

About Graph Execution

Aviv C edited this page Jul 1, 2017 · 3 revisions

So what exactly is graph execution? Since graph execution is the core of the JavaRed library, first, let's discuss the what and why.

Execution Levels

We will define 3 different levels of concurrent executions:

  1. Linear Execution.
  2. Concurrent Execution.
  3. Graph Execution.

Linear 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:

Linear Execution Example

Blocking implementation:

preformTaskA();
preformTaskB();
preformTaskC();
preformTaskD();

Non-blocking implementation:

preformTaskA().whenFinished(() -> {
	preformTaskB().whenFinished(() -> {
		preformTaskC().whenFinished(() -> {
			preformTaskD();
		});
	});
});

Concurrent Execution

A concurrent execution means that we can execute multiple tasks at the same time.

Example:

Concurrent Execution 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

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:

Graph Execution Example

This type of execution cannot be implemented using blocking code. The non blocking example will be shown in the Graph module section.