Skip to content

Commit

Permalink
Merge pull request #1576 from colinkho:main
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 657990422
  • Loading branch information
copybara-github committed Jul 31, 2024
2 parents 6e678e5 + c637774 commit 40de898
Showing 1 changed file with 21 additions and 5 deletions.
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

0 comments on commit 40de898

Please sign in to comment.