@@ -403,20 +403,39 @@ public void run() {
403
403
404
404
/** A builder for {@link GlideExecutor}s. */
405
405
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
+
406
414
private int corePoolSize ;
407
415
private int maximumPoolSize ;
408
- private final boolean preventNetworkOperations ;
409
416
410
417
@ NonNull
411
418
private UncaughtThrowableStrategy uncaughtThrowableStrategy = UncaughtThrowableStrategy .DEFAULT ;
412
419
413
420
private String name ;
421
+ private long threadTimeoutMillis ;
414
422
415
423
@ Synthetic
416
424
Builder (boolean preventNetworkOperations ) {
417
425
this .preventNetworkOperations = preventNetworkOperations ;
418
426
}
419
427
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
+
420
439
/** Sets the maximum number of threads to use. */
421
440
public Builder setThreadCount (@ IntRange (from = 1 ) int threadCount ) {
422
441
corePoolSize = threadCount ;
@@ -448,14 +467,20 @@ public GlideExecutor build() {
448
467
throw new IllegalArgumentException (
449
468
"Name must be non-null and non-empty, but given: " + name );
450
469
}
451
- return new GlideExecutor (
470
+ ThreadPoolExecutor executor =
452
471
new ThreadPoolExecutor (
453
472
corePoolSize ,
454
473
maximumPoolSize ,
455
- /*keepAliveTime=*/ 0L ,
474
+ /*keepAliveTime=*/ threadTimeoutMillis ,
456
475
TimeUnit .MILLISECONDS ,
457
476
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 );
459
484
}
460
485
}
461
486
}
0 commit comments