@@ -3,6 +3,7 @@ An API that allows you to execute, parallelize, and async code dynamically, and
33
44## WARNING**
55> If you're seeing this message the README.md isn't entirely up-to-date, it will be up-to-date shortly.
6+ > The main things un-updated are the definitions for methods, however the java docs ** ARE** up to date, reference them if you have any question.
67
78> [ !NOTE]
89> > This is a shadable project.
@@ -26,6 +27,18 @@ This is how to add this project as either a dependency, or just using it outrigh
2627> This will direct you to the jitpack page, this will instruct you on how to apply
2728> this in your pom, or pom equivalent, allowing you to use the API itself.
2829> I ** HIGHLY** Recommend using the latest commit. If you must, avoid ** EXPERIMENTAL** code.
30+ ## Initialization
31+ This is even more important than the following, as if you don't enable it; it simply won't work!
32+
33+ ``` java
34+ import org.DynamicThread.TaskManager ;
35+
36+ void main() {
37+ TaskManager . enable();
38+ TaskManager . enable(Settings . .. args);
39+ // / Reference the java-doc if you're confused about the overload of .enable(), which has arguments.
40+ }
41+ ```
2942
3043## Deinitialization
3144This is a very important step if you're using this for any project with processes that effect persistent data.
@@ -84,9 +97,14 @@ While there are sparse features, there is a large sum of methods you can use to
8497> For this method there are actually quite a few overloads, they can be seen below.
8598>
8699> ``` java
87- > AsyncTask . of(Runnable . .. runnables);
88- > AsyncTask . of(Supplier<?> supplier, Runnable . .. runnables);
89- > AsyncTask . of(Supplier<?> supplier);
100+ > import org.DynamicThread.Parallel ;
101+ >
102+ > Async . of();
103+ > Async . of(Supplier<?> supplier);
104+ > AsyncTask . of(Supplier<?> supplier, Async<?> async);
105+ > AsyncTask . of(Supplier<?> supplier, Parallel<?> parallel);
106+ > AsyncTask . of(Parallel<?> parallel);
107+ > AsyncTask . of(Async async);
90108> ```
91109> For each of these respectively they all have different functionalities.
92110> 1. The first, this simply executes all supplied tasks in parallel, and then calls all hooked tasks (see ** Hooking Tasks ** )
@@ -98,14 +116,16 @@ While there are sparse features, there is a large sum of methods you can use to
98116> This specific tab is going to have A ** LOT ** of methods, and even more overloads.
99117> Each method has slightly different function then its parent method.
100118> ```java
101- > AsyncTask . after(Runnable . .. runnables);
102- > AsyncTask . after(Consumer<?> ... consumers);
103- > AsyncTask . after(Function<?,?> function, Consumer<?> ... consumers);
119+ > import org.DynamicThread.Parallel ;
120+ >
121+ > AsyncTask . after(Async<?> async);
122+ > AsyncTask . after(Parallel<?> parallel);
104123> AsyncTask . after(Supplier<?> supplier);
105- > AsyncTask . after(Supplier<?> supplier, Runnable . .. runnables);
106- > AsyncTask . after(Supplier<?> supplier, Consumer<?> ... consumers);
107- > AsyncTask . after(Function<?,?> function,Runnable . .. runnables);
124+ > AsyncTask . after(Supplier<?> supplier, Async<?> async);
125+ > AsyncTask . after(Supplier<?> supplier, Parallel<?> parallel);
108126> AsyncTask . after(Function<?,?> function);
127+ > AsyncTask . after(Function<?,?> function, Async<?> async);
128+ > AsyncTask . after(Function<?,?> function, Parallel<?> parallel);
109129> ```
110130> Naturally these will all have to be explained,
111131> 1. The first, will run all the submitted tasks in parallel after the previous task is completed
@@ -175,9 +195,6 @@ While there are sparse features, there is a large sum of methods you can use to
175195> AsyncTask . cancel();
176196> AsyncTask . join();
177197> AsyncTask . join(long time);
178- > AsyncTask . collect();
179- > AsyncTask . collet(long time);
180- > AsyncTask . cancel(long time);
181198> ```
182199> 1. The first, returns true if the task is completed.
183200> 2. The second, returns true if the task is cancelled.
@@ -192,60 +209,41 @@ While there are sparse features, there is a large sum of methods you can use to
192209
193210## Examples
194211> ```java
212+ > import org.DynamicThread.AsyncTask ;
213+ > import org.DynamicThread.TaskManager ;
214+ >
215+ > static int fib(int n) {
216+ > if (n <= 1 ) return n;
217+ > return fib(n - 1 ) + fib(n - 2 );
218+ > }
219+ >
220+ > @SuppressWarnings (" CodeBlock2Expr" )
195221> void main() throws InterruptedException {
196- > AsyncTask<String > task = AsyncTask . of(
197- > () - > 10 ,
198- > () - > System . out. println(" Task 1" ), // shows that the tasks are parallelized
199- > () - > System . out. println(" Task 2" ),
200- > () - > System . out. println(" Task 3" ),
201- > () - > System . out. println(" Task 4" ),
202- > () - > System . out. println(" Task 5" )
203- > ) // creates a returnable of type integer
204- >
205- > .onStart(() - > {
206- > System . out. println(" Started main task" );
207- > }) // shows when the task starts
208- >
209- > .after((item) - > {
210- > return item + 3 ;
211- > }) // returns 13 (doesn't change return type)
212- >
213- > .after((item) - > {
214- > System . out. println(" Resulted in " + item);
215- > System . out. println(" Changed type to a runnable" );
216- > }) // converts to runnable
217- >
218- > .after(() - > {
219- > System . out. println(" Changed type back to returnable, its type is String" );
220- > return " capybara" ;
221- > }) // converts to returnable of type string
222- >
223- > .onComplete((item) - > {
224- > System . out. println(" Entire task completed with result " + item);
225- > }) // calls after the entire task is completed
226- > .start();
227- >
228- > System . out. println(" Sleep Started" );
229- > Thread . sleep(1500 ); // proves it's actually asynced
230- > System . out. println(" Sleep Ended" );
231- > System . out. println(" Collecting results of the task back into main thread with result: " + task. collect());
222+ > Thread . sleep(2000 ); // jvm warmup
223+ > final long time = System . currentTimeMillis();
224+ > TaskManager . enable();
225+ > System . out. println(" === Test Started ===" );
226+ > for (int i = 0 ; i < 5_000 ; i++ ) {
227+ > AsyncTask . of(() - > fib(20 ))
228+ > .onComplete((item) - > {
229+ > System . out. println(" Result of " + item + " Process took, " + (time - System . currentTimeMillis()) + " ms" );
230+ > })
231+ > .start();
232+ > }
233+ > System . out. println(" === Sleeping to allow async work ===" );
234+ > Thread . sleep(5000 );
235+ > System . out. println(" === Test Complete ===" );
236+ > TaskManager . disable();
232237> }
238+ >
233239> ```
234240> ## Console Output
235241> The console output can be seen below, this naturally proves all I ' ve depicted in the code.
236242> ```.shell
237- > Started main task
238- > Sleep Started
239- > Task 1
240- > Task 5
241- > Task 3
242- > Task 2
243- > Task 4
244- > Resulted in 13
245- > Changed type to a runnable
246- > Changed type back to returnable, its type is String
247- > Entire task completed with result capybara
248- > Sleep Ended
249- > Collecting results of the task back into main thread with result: capybara
243+ > Result of 6765 Process took, -16ms
244+ > ....
245+ > (there was alot of output)
246+ > ....
247+ > Result of 6765 Process took, -327ms
250248> ```
251-
249+ > It is not to be ignored that this is relative to an epoch, so over 5000 fib(20) operations, it executed in under a single second.
0 commit comments