Created
February 1, 2021 16:08
-
-
Save jstefanelli/5fc432431511486394d5b9bfbbe7de0f to your computer and use it in GitHub Desktop.
A simple task (with subtasks) implemented in my custom TPL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Task<Boolean> maintask = new Task<Boolean>(myContext) { | |
| int internalState = 0; | |
| @Override | |
| public AwaitableState work() { | |
| switch(internalState){ | |
| case 0: | |
| return Work0(); | |
| case 1: | |
| return Work1(); | |
| case 2: | |
| return Work2(); | |
| } | |
| return AwaitableState.Completed; | |
| } | |
| private AwaitableState Work0(){ | |
| internalState = 1; | |
| ArrayList<Awaitable> workers = new ArrayList<>(); | |
| for(int i = 0; i < 100; i++){ | |
| final int x = i; | |
| Task<Boolean> t = new Task<Boolean>() { | |
| @Override | |
| public AwaitableState work() { | |
| System.out.println("Work Item " + x + " is working on thread: " + Thread.currentThread().getId()); | |
| return AwaitableState.Completed; | |
| } | |
| }; | |
| context.submit(t); | |
| workers.add(t); | |
| } | |
| return waitFor(Task.whenAll(workers, context)); | |
| } | |
| private AwaitableState Work1(){ | |
| internalState = 2; | |
| ArrayList<Awaitable> workers = new ArrayList<>(); | |
| for(int i = 0; i < 100; i++){ | |
| final int x = 100 + i; | |
| Task<Boolean> t = new Task<Boolean>() { | |
| @Override | |
| public AwaitableState work() { | |
| System.out.println("Work Item " + x + " is working!"); | |
| return AwaitableState.Completed; | |
| } | |
| }; | |
| context.submit(t); | |
| workers.add(t); | |
| } | |
| return waitFor(Task.whenAll(workers, context)); | |
| } | |
| private AwaitableState Work2(){ | |
| this.retVal = true; | |
| return AwaitableState.Completed; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment