public static TaskFactory finalizeTaskFactory(TaskFactory factory, ExecutorService taskThreadPool) {
    validateFactory(factory);
    boolean isAsyncTaskClass = factory instanceof AsyncStreamTaskFactory;

    if (isAsyncTaskClass) {
      log.info("Got an AsyncStreamTask implementation.");
      return factory;
    }

    log.info("Converting StreamTask to AsyncStreamTaskAdapter");
    return (AsyncStreamTaskFactory) () ->
        new AsyncStreamTaskAdapter(((StreamTaskFactory) factory).createInstance(), taskThreadPool);
  }
Optionally wrap the {@link StreamTaskFactory} in a {@link AsyncStreamTaskFactory}, when running {@link StreamTask}
in multi-thread mode.

@param factory  the task factory instance loaded according to the task class
@param taskThreadPool  the thread pool to run the {@link AsyncStreamTaskAdapter} tasks
@return  the finalized task factory object
samza-core/src/main/java/org/apache/samza/task/TaskFactoryUtil.java