Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom ExecutorService to be supplied to the Loader #1576

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;

Expand Down Expand Up @@ -204,18 +205,33 @@ public boolean isRetry() {
}
}

private final ExecutorService downloadExecutorService;
private final Executor downloadExecutor;
private final Runnable downloadExecutorReleaser;

@Nullable private LoadTask<? extends Loadable> currentTask;
@Nullable private IOException fatalError;

/**
* Constructs an instance.
*
* @param threadNameSuffix A name suffix for the loader's thread. This should be the name of the
* component using the loader.
*/
public Loader(String threadNameSuffix) {
this.downloadExecutorService =
ExecutorService executorService =
Util.newSingleThreadExecutor(THREAD_NAME_PREFIX + threadNameSuffix);
this.downloadExecutor = executorService;
this.downloadExecutorReleaser = executorService::shutdown;
}

/**
* Constructs an instance.
*
* @param downloadExecutor An {@link Executor} for supplying the loader's thread.
*/
public Loader(Executor downloadExecutor) {
this.downloadExecutor = downloadExecutor;
this.downloadExecutorReleaser = () -> {};
}

/**
Expand Down Expand Up @@ -297,9 +313,9 @@ public void release(@Nullable ReleaseCallback callback) {
currentTask.cancel(true);
}
if (callback != null) {
downloadExecutorService.execute(new ReleaseTask(callback));
downloadExecutor.execute(new ReleaseTask(callback));
}
downloadExecutorService.shutdown();
downloadExecutorReleaser.run();
}

// LoaderErrorThrower implementation.
Expand Down Expand Up @@ -516,7 +532,7 @@ public void handleMessage(Message msg) {

private void execute() {
currentError = null;
downloadExecutorService.execute(Assertions.checkNotNull(currentTask));
downloadExecutor.execute(Assertions.checkNotNull(currentTask));
}

private void finish() {
Expand Down