Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Roberto-Gentili
Last active October 24, 2021 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Roberto-Gentili/1a67eaba558947ae99f9c44077153870 to your computer and use it in GitHub Desktop.
Save Roberto-Gentili/1a67eaba558947ae99f9c44077153870 to your computer and use it in GitHub Desktop.
import static org.burningwave.core.assembler.StaticComponentContainer.BackgroundExecutor;
import org.burningwave.core.ManagedLogger;
import org.burningwave.core.concurrent.QueuedTasksExecutor.ProducerTask;
import org.burningwave.core.concurrent.QueuedTasksExecutor.Task;
public class TaskLauncher implements ManagedLogger {
public void launch() {
ProducerTask<Long> taskOne = BackgroundExecutor.createProducerTask(task -> {
Long startTime = System.currentTimeMillis();
logInfo("task one started");
synchronized (this) {
wait(5000);
}
Task internalTask = BackgroundExecutor.createTask(tsk -> {
logInfo("internal task started");
synchronized (this) {
wait(5000);
}
logInfo("internal task finished");
}, Thread.MAX_PRIORITY).submit();
internalTask.waitForFinish();
logInfo("task one finished");
return startTime;
}, Thread.MAX_PRIORITY);
taskOne.submit();
Task taskTwo = BackgroundExecutor.createTask(task -> {
logInfo("task two started and wait for task one finishing");
taskOne.waitForFinish();
logInfo("task two finished");
}, Thread.NORM_PRIORITY);
taskTwo.submit();
ProducerTask<Long> taskThree = BackgroundExecutor.createProducerTask(task -> {
logInfo("task three started and wait for task two finishing");
taskTwo.waitForFinish();
logInfo("task two finished");
return System.currentTimeMillis();
}, Thread.MIN_PRIORITY);
taskThree.submit();
taskThree.waitForFinish();
logInfo("Elapsed time: {}ms", taskThree.join() - taskOne.join());
}
public static void main(String[] args) {
new TaskLauncher().launch();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment