Skip to content

Commit

Permalink
Add a thread timeout method to GlideExecutor's builders.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 270305260
  • Loading branch information
sjudd authored and glide-copybara-robot committed Sep 20, 2019
1 parent f9a7966 commit db3acef
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
org.gradle.daemon=true
org.gradle.configureondemand=false

VERSION_NAME=4.11.0-SNAPSHOT
VERSION_NAME=4.10.0-SNAPSHOT
VERSION_MAJOR=4
VERSION_MINOR=11
VERSION_MINOR=10
VERSION_PATCH=0
GROUP=com.github.bumptech.glide

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,20 +403,39 @@ public void run() {

/** A builder for {@link GlideExecutor}s. */
public static final class Builder {
/**
* Prevents core and non-core threads from timing out ever if provided to {@link
* #setThreadTimeoutMillis(long)}.
*/
public static final long NO_THREAD_TIMEOUT = 0L;

private final boolean preventNetworkOperations;

private int corePoolSize;
private int maximumPoolSize;
private final boolean preventNetworkOperations;

@NonNull
private UncaughtThrowableStrategy uncaughtThrowableStrategy = UncaughtThrowableStrategy.DEFAULT;

private String name;
private long threadTimeoutMillis;

@Synthetic
Builder(boolean preventNetworkOperations) {
this.preventNetworkOperations = preventNetworkOperations;
}

/**
* Allows both core and non-core threads in the executor to be terminated if no tasks arrive for
* at least the given timeout milliseconds.
*
* <p>Use {@link #NO_THREAD_TIMEOUT} to remove a previously set timeout.
*/
public Builder setThreadTimeoutMillis(long threadTimeoutMillis) {
this.threadTimeoutMillis = threadTimeoutMillis;
return this;
}

/** Sets the maximum number of threads to use. */
public Builder setThreadCount(@IntRange(from = 1) int threadCount) {
corePoolSize = threadCount;
Expand Down Expand Up @@ -448,14 +467,20 @@ public GlideExecutor build() {
throw new IllegalArgumentException(
"Name must be non-null and non-empty, but given: " + name);
}
return new GlideExecutor(
ThreadPoolExecutor executor =
new ThreadPoolExecutor(
corePoolSize,
maximumPoolSize,
/*keepAliveTime=*/ 0L,
/*keepAliveTime=*/ threadTimeoutMillis,
TimeUnit.MILLISECONDS,
new PriorityBlockingQueue<Runnable>(),
new DefaultThreadFactory(name, uncaughtThrowableStrategy, preventNetworkOperations)));
new DefaultThreadFactory(name, uncaughtThrowableStrategy, preventNetworkOperations));

if (threadTimeoutMillis != NO_THREAD_TIMEOUT) {
executor.allowCoreThreadTimeOut(true);
}

return new GlideExecutor(executor);
}
}
}

0 comments on commit db3acef

Please sign in to comment.