Skip to content

Commit

Permalink
Merge pull request #2242 from marunjar/fix_thread_pool
Browse files Browse the repository at this point in the history
fix threadpool
  • Loading branch information
marunjar authored Jan 21, 2024
2 parents 92f3393 + 5f31a1d commit 937210b
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions app/src/main/java/fr/neamar/kiss/utils/Utilities.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.concurrent.Executor;

public class Utilities {
final static Executor EXECUTOR_RUN_ASYNC = AsyncTask.THREAD_POOL_EXECUTOR;

/**
* Return a valid activity or null given a view
Expand Down Expand Up @@ -53,7 +52,26 @@ public static Activity getActivity(@Nullable Context ctx) {
}

public static Utilities.AsyncRun runAsync(@NonNull AsyncRun.Run background, @Nullable AsyncRun.Run after) {
return (Utilities.AsyncRun) new Utilities.AsyncRun(background, after).executeOnExecutor(EXECUTOR_RUN_ASYNC);
return runAsync(background, after, getDefaultExecutor());
}

public static Utilities.AsyncRun runAsync(@NonNull AsyncRun.Run background, @Nullable AsyncRun.Run after, @NonNull Executor exec) {
return (Utilities.AsyncRun) new Utilities.AsyncRun(background, after).executeOnExecutor(exec);
}

/**
* Get default executor for async operations.
* From android Q AsyncTask.THREAD_POOL_EXECUTOR supports fallback if queue gets too long.
* Else we use more safe AsyncTask.SERIAL_EXECUTOR.
*
* @return Executor
*/
public static Executor getDefaultExecutor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return AsyncTask.THREAD_POOL_EXECUTOR;
} else {
return AsyncTask.SERIAL_EXECUTOR;
}
}

public static class AsyncRun extends AsyncTask<Void, Void, Void> {
Expand Down

0 comments on commit 937210b

Please sign in to comment.