From 68e65ec884f0146bf4ed2e5c9a2ed265c738cbe9 Mon Sep 17 00:00:00 2001 From: Colin Kho Date: Tue, 30 Jul 2024 13:20:30 -0700 Subject: [PATCH 1/3] Allow custom ExecutorService to be supplied to the Loader --- .../androidx/media3/exoplayer/upstream/Loader.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java index 71212e7eea1..b7ce5d59169 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java @@ -214,8 +214,14 @@ public boolean isRetry() { * component using the loader. */ public Loader(String threadNameSuffix) { - this.downloadExecutorService = - Util.newSingleThreadExecutor(THREAD_NAME_PREFIX + threadNameSuffix); + this(Util.newSingleThreadExecutor(THREAD_NAME_PREFIX + threadNameSuffix)); + } + + /** + * @param downloadExecutorService An {@link ExecutorService} for supplying the loader's thread. + */ + public Loader(ExecutorService downloadExecutorService) { + this.downloadExecutorService = downloadExecutorService; } /** From ff22838c0d20ae781ad3b1f02ae702c0016b3aee Mon Sep 17 00:00:00 2001 From: Ian Baker Date: Wed, 31 Jul 2024 09:59:48 +0100 Subject: [PATCH 2/3] Add javadoc summary fragment --- .../main/java/androidx/media3/exoplayer/upstream/Loader.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java index b7ce5d59169..a0edb917e26 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java @@ -210,6 +210,8 @@ public boolean isRetry() { @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. */ @@ -218,6 +220,8 @@ public Loader(String threadNameSuffix) { } /** + * Constructs an instance. + * * @param downloadExecutorService An {@link ExecutorService} for supplying the loader's thread. */ public Loader(ExecutorService downloadExecutorService) { From c637774cc25be7e15e853615940813cf1afb0469 Mon Sep 17 00:00:00 2001 From: Ian Baker Date: Wed, 31 Jul 2024 10:33:57 +0100 Subject: [PATCH 3/3] Take Executor instead of ExecutorService This also avoids shutting down the externally-provided ExecutorService, which we shouldn't do (since we don't really own it, and didn't create it). --- .../media3/exoplayer/upstream/Loader.java | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java index a0edb917e26..8fc3ffecb31 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/upstream/Loader.java @@ -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; @@ -204,7 +205,8 @@ public boolean isRetry() { } } - private final ExecutorService downloadExecutorService; + private final Executor downloadExecutor; + private final Runnable downloadExecutorReleaser; @Nullable private LoadTask currentTask; @Nullable private IOException fatalError; @@ -216,16 +218,20 @@ public boolean isRetry() { * component using the loader. */ public Loader(String threadNameSuffix) { - this(Util.newSingleThreadExecutor(THREAD_NAME_PREFIX + threadNameSuffix)); + ExecutorService executorService = + Util.newSingleThreadExecutor(THREAD_NAME_PREFIX + threadNameSuffix); + this.downloadExecutor = executorService; + this.downloadExecutorReleaser = executorService::shutdown; } /** * Constructs an instance. * - * @param downloadExecutorService An {@link ExecutorService} for supplying the loader's thread. + * @param downloadExecutor An {@link Executor} for supplying the loader's thread. */ - public Loader(ExecutorService downloadExecutorService) { - this.downloadExecutorService = downloadExecutorService; + public Loader(Executor downloadExecutor) { + this.downloadExecutor = downloadExecutor; + this.downloadExecutorReleaser = () -> {}; } /** @@ -307,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. @@ -526,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() {