Stay organized with collections
Save and categorize content based on your preferences.
DiscardOldestPolicy
open class DiscardOldestPolicy : RejectedExecutionHandler
A handler for rejected tasks that discards the oldest unhandled request and then retries execute
, unless the executor is shut down, in which case the task is discarded. This policy is rarely useful in cases where other threads may be waiting for tasks to terminate, or failures must be recorded. Instead consider using a handler of the form:
<code>new RejectedExecutionHandler() {
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
Runnable dropped = e.getQueue().poll();
if (dropped instanceof Future<?>) {
((Future<?>)dropped).cancel(false);
// also consider logging the failure
}
e.execute(r); // retry
}}</code>
Summary
Public constructors |
Creates a DiscardOldestPolicy for the given executor.
|
Public methods |
open Unit |
Obtains and ignores the next task that the executor would otherwise execute, if one is immediately available, and then retries execution of task r, unless the executor is shut down, in which case task r is instead discarded.
|
Public constructors
DiscardOldestPolicy
DiscardOldestPolicy()
Creates a DiscardOldestPolicy
for the given executor.
Public methods
rejectedExecution
open fun rejectedExecution(
r: Runnable!,
e: ThreadPoolExecutor!
): Unit
Obtains and ignores the next task that the executor would otherwise execute, if one is immediately available, and then retries execution of task r, unless the executor is shut down, in which case task r is instead discarded.
Parameters |
r |
Runnable!: the runnable task requested to be executed |
executor |
the executor attempting to execute this task |
e |
ThreadPoolExecutor!: the executor attempting to execute this task |
Exceptions |
java.util.concurrent.RejectedExecutionException |
if there is no remedy |
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[null,null,["Last updated 2025-02-10 UTC."],[],[],null,["# ThreadPoolExecutor.DiscardOldestPolicy\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)\n\nDiscardOldestPolicy\n===================\n\n```\nopen class DiscardOldestPolicy : RejectedExecutionHandler\n```\n\n|---|------------------------------------------------------------------|\n| [kotlin.Any](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html) ||\n| ↳ | [java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy](#) |\n\nA handler for rejected tasks that discards the oldest unhandled request and then retries `execute`, unless the executor is shut down, in which case the task is discarded. This policy is rarely useful in cases where other threads may be waiting for tasks to terminate, or failures must be recorded. Instead consider using a handler of the form: \n\n```kotlin\n\u003ccode\u003enew RejectedExecutionHandler() {\n public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {\n Runnable dropped = e.getQueue().poll();\n if (dropped instanceof Future<?>) {\n ((Future<?>)dropped).cancel(false);\n // also consider logging the failure\n }\n e.execute(r); // retry\n }}\u003c/code\u003e\n```\n\n\u003cbr /\u003e\n\nSummary\n-------\n\n| Public constructors ||\n|-----------------------------------------------------------------------------------------------------------|---|\n| [DiscardOldestPolicy](#DiscardOldestPolicy())`()` Creates a `DiscardOldestPolicy` for the given executor. |\n\n| Public methods ||\n|-----------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| open [Unit](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-unit/index.html) | [rejectedExecution](#rejectedExecution(java.lang.Runnable,%20java.util.concurrent.ThreadPoolExecutor))`(`r:` `[Runnable](../../lang/Runnable.html#)!`, `e:` `[ThreadPoolExecutor](/reference/kotlin/java/util/concurrent/ThreadPoolExecutor)!`)` Obtains and ignores the next task that the executor would otherwise execute, if one is immediately available, and then retries execution of task r, unless the executor is shut down, in which case task r is instead discarded. |\n\nPublic constructors\n-------------------\n\n### DiscardOldestPolicy\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nDiscardOldestPolicy()\n```\n\nCreates a `DiscardOldestPolicy` for the given executor.\n\nPublic methods\n--------------\n\n### rejectedExecution\n\nAdded in [API level 1](https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels) \n\n```\nopen fun rejectedExecution(\n r: Runnable!, \n e: ThreadPoolExecutor!\n): Unit\n```\n\nObtains and ignores the next task that the executor would otherwise execute, if one is immediately available, and then retries execution of task r, unless the executor is shut down, in which case task r is instead discarded.\n\n| Parameters ||\n|------------|--------------------------------------------------------------------------------------------------------------------------------|\n| `r` | [Runnable](../../lang/Runnable.html#)!: the runnable task requested to be executed |\n| `executor` | the executor attempting to execute this task |\n| `e` | [ThreadPoolExecutor](/reference/kotlin/java/util/concurrent/ThreadPoolExecutor)!: the executor attempting to execute this task |\n\n| Exceptions ||\n|---------------------------------------------------|-----------------------|\n| `java.util.concurrent.RejectedExecutionException` | if there is no remedy |"]]