-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #17 from ghorbani-m/main
feat:add multi-thread to android module.
- Loading branch information
Showing
2 changed files
with
90 additions
and
35 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package land.fx.fula; | ||
|
||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.Future; | ||
|
||
final class ThreadUtils { | ||
/** | ||
* Thread which will be used to call all Libp2p APIs. They | ||
* they don't run on the calling thread anyway, we are deferring the calls | ||
* to this thread to avoid (potentially) blocking the calling thread. | ||
*/ | ||
private static final ExecutorService executor | ||
= Executors.newSingleThreadExecutor(); | ||
|
||
/** | ||
* Runs the given {@link Runnable} on the executor. | ||
* @param runnable | ||
*/ | ||
public static void runOnExecutor(Runnable runnable) { | ||
executor.execute(runnable); | ||
} | ||
|
||
/** | ||
* Submits the given {@link Callable} to be run on the executor. | ||
* @param callable | ||
* @return Future. | ||
*/ | ||
public static <T> Future<T> submitToExecutor(Callable<T> callable) { | ||
return executor.submit(callable); | ||
} | ||
|
||
/** | ||
* Submits the given {@link Runnable} to be run on the executor. | ||
* @param runnable | ||
* @return Future. | ||
*/ | ||
public static Future<?> submitToExecutor(Runnable runnable) { | ||
return executor.submit(runnable); | ||
} | ||
} |