Skip to content

Commit db3acef

Browse files
sjuddglide-copybara-robot
authored andcommittedSep 20, 2019
Add a thread timeout method to GlideExecutor's builders.
PiperOrigin-RevId: 270305260
1 parent f9a7966 commit db3acef

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed
 

‎gradle.properties

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
org.gradle.daemon=true
22
org.gradle.configureondemand=false
33

4-
VERSION_NAME=4.11.0-SNAPSHOT
4+
VERSION_NAME=4.10.0-SNAPSHOT
55
VERSION_MAJOR=4
6-
VERSION_MINOR=11
6+
VERSION_MINOR=10
77
VERSION_PATCH=0
88
GROUP=com.github.bumptech.glide
99

‎library/src/main/java/com/bumptech/glide/load/engine/executor/GlideExecutor.java

+29-4
Original file line numberDiff line numberDiff line change
@@ -403,20 +403,39 @@ public void run() {
403403

404404
/** A builder for {@link GlideExecutor}s. */
405405
public static final class Builder {
406+
/**
407+
* Prevents core and non-core threads from timing out ever if provided to {@link
408+
* #setThreadTimeoutMillis(long)}.
409+
*/
410+
public static final long NO_THREAD_TIMEOUT = 0L;
411+
412+
private final boolean preventNetworkOperations;
413+
406414
private int corePoolSize;
407415
private int maximumPoolSize;
408-
private final boolean preventNetworkOperations;
409416

410417
@NonNull
411418
private UncaughtThrowableStrategy uncaughtThrowableStrategy = UncaughtThrowableStrategy.DEFAULT;
412419

413420
private String name;
421+
private long threadTimeoutMillis;
414422

415423
@Synthetic
416424
Builder(boolean preventNetworkOperations) {
417425
this.preventNetworkOperations = preventNetworkOperations;
418426
}
419427

428+
/**
429+
* Allows both core and non-core threads in the executor to be terminated if no tasks arrive for
430+
* at least the given timeout milliseconds.
431+
*
432+
* <p>Use {@link #NO_THREAD_TIMEOUT} to remove a previously set timeout.
433+
*/
434+
public Builder setThreadTimeoutMillis(long threadTimeoutMillis) {
435+
this.threadTimeoutMillis = threadTimeoutMillis;
436+
return this;
437+
}
438+
420439
/** Sets the maximum number of threads to use. */
421440
public Builder setThreadCount(@IntRange(from = 1) int threadCount) {
422441
corePoolSize = threadCount;
@@ -448,14 +467,20 @@ public GlideExecutor build() {
448467
throw new IllegalArgumentException(
449468
"Name must be non-null and non-empty, but given: " + name);
450469
}
451-
return new GlideExecutor(
470+
ThreadPoolExecutor executor =
452471
new ThreadPoolExecutor(
453472
corePoolSize,
454473
maximumPoolSize,
455-
/*keepAliveTime=*/ 0L,
474+
/*keepAliveTime=*/ threadTimeoutMillis,
456475
TimeUnit.MILLISECONDS,
457476
new PriorityBlockingQueue<Runnable>(),
458-
new DefaultThreadFactory(name, uncaughtThrowableStrategy, preventNetworkOperations)));
477+
new DefaultThreadFactory(name, uncaughtThrowableStrategy, preventNetworkOperations));
478+
479+
if (threadTimeoutMillis != NO_THREAD_TIMEOUT) {
480+
executor.allowCoreThreadTimeOut(true);
481+
}
482+
483+
return new GlideExecutor(executor);
459484
}
460485
}
461486
}

0 commit comments

Comments
 (0)
Please sign in to comment.