Skip to main content

Child Threads

You can use WorkflowThread#spawnThread() and WorkflowThread#waitForThreads() to launch and wait for Child ThreadRuns, respectively. This is useful if you want to execute multiple pieces of business logic in parallel.

Spawning Child Threads

The WorkflowThread::spawnThread() method takes in three arguments:

  1. A ThreadFunc (normally a lambda or function pointer) defining the logic for the Child ThreadSpec/ThreadRun.
  2. The name to assign to the Child ThreadSpec.
  3. A Map<String, ?> for any input variables to the child thread (or equivalent, depending on the language of the SDK you use).

Let's spawn a child thread whose ThreadSpec we call my-child-thread, and set the variable child-var to foo.

SpawnedThread spawnedThread = thread.spawnThread(
child -> {
WfRunVariable childVar = child.addVariable("child-var", VariableType.STR);
child.execute("some-task", childVar);
},
"my-child-thread",
Map.of("child-var", "foo")
);

Waiting for Child Threads

The WorkflowThread::waitForThreads() method takes in a variable number of args. Each arg is the SpawnedThread object returned when you launch your Child ThreadRun (see above).

note

The return type is NodeOutput; however, it should be noted that the NodeOutput should only be used to set a timeout or handle an exception; there is no content/payload/value associated with NodeOutput for WAIT_FOR_THREAD Nodes.

Future releases of LittleHorse will allow a child ThreadRun to return an output.

SpawnedThread spawnedThread = thread.spawnThread(...);
SpawnedThread anotherThread = thread.spawnThread(...);
// Omitted: Execute some business logic in parallel

NodeOutput waitedThreads = thread.waitForThreads(spawnedThread, anotherThread);

// You can handle exceptions here
thread.handleException(
waitedThread,
null, // catch any failure
handler -> {
handler.execute("some-error-reporting-task");
}
);