RejectedExecutionHandler
Stay organized with collections
Save and categorize content based on your preferences.
Known Indirect Subclasses
|
Public Methods
public
abstract
void
rejectedExecution
(Runnable r, ThreadPoolExecutor executor)
Method that may be invoked by a ThreadPoolExecutor
when
execute
cannot accept a
task. This may occur when no more threads or queue slots are
available because their bounds would be exceeded, or upon
shutdown of the Executor.
In the absence of other alternatives, the method may throw
an unchecked RejectedExecutionException
, which will be
propagated to the caller of execute
.
Parameters
r |
the runnable task requested to be executed |
executor |
the executor attempting to execute this task |
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["\u003cp\u003e\u003ccode\u003eRejectedExecutionHandler\u003c/code\u003e is an interface used to handle tasks that a \u003ccode\u003eThreadPoolExecutor\u003c/code\u003e cannot execute.\u003c/p\u003e\n"],["\u003cp\u003eIt defines a single method, \u003ccode\u003erejectedExecution\u003c/code\u003e, which is invoked when a task is rejected.\u003c/p\u003e\n"],["\u003cp\u003eImplementations of this interface provide custom logic for handling rejected tasks, such as discarding, re-queuing, or throwing an exception.\u003c/p\u003e\n"],["\u003cp\u003eFour built-in implementations are available: \u003ccode\u003eAbortPolicy\u003c/code\u003e, \u003ccode\u003eCallerRunsPolicy\u003c/code\u003e, \u003ccode\u003eDiscardOldestPolicy\u003c/code\u003e, and \u003ccode\u003eDiscardPolicy\u003c/code\u003e.\u003c/p\u003e\n"],["\u003cp\u003e\u003ccode\u003erejectedExecution\u003c/code\u003e is called when a \u003ccode\u003eThreadPoolExecutor\u003c/code\u003e is saturated or shut down, providing a mechanism to manage task overflow.\u003c/p\u003e\n"]]],[],null,["# RejectedExecutionHandler\n\npublic interface **RejectedExecutionHandler** \n\n|---|---|---|\n| Known Indirect Subclasses [ThreadPoolExecutor.AbortPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.AbortPolicy.html), [ThreadPoolExecutor.CallerRunsPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.CallerRunsPolicy.html), [ThreadPoolExecutor.DiscardOldestPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.DiscardOldestPolicy.html), [ThreadPoolExecutor.DiscardPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.DiscardPolicy.html) |----------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [ThreadPoolExecutor.AbortPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.AbortPolicy.html) | A handler for rejected tasks that throws a `RejectedExecutionException`. | | [ThreadPoolExecutor.CallerRunsPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.CallerRunsPolicy.html) | A handler for rejected tasks that runs the rejected task directly in the calling thread of the `execute` method, unless the executor has been shut down, in which case the task is discarded. | | [ThreadPoolExecutor.DiscardOldestPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.DiscardOldestPolicy.html) | 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. | | [ThreadPoolExecutor.DiscardPolicy](../../../../reference/java/util/concurrent/ThreadPoolExecutor.DiscardPolicy.html) | A handler for rejected tasks that silently discards the rejected task. | |||\n\nA handler for tasks that cannot be executed by a [ThreadPoolExecutor](../../../../reference/java/util/concurrent/ThreadPoolExecutor.html). \n\n### Public Method Summary\n\n|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| abstract void | [rejectedExecution](../../../../reference/java/util/concurrent/RejectedExecutionHandler.html#rejectedExecution(java.lang.Runnable,%20java.util.concurrent.ThreadPoolExecutor))([Runnable](../../../../reference/java/lang/Runnable.html) r, [ThreadPoolExecutor](../../../../reference/java/util/concurrent/ThreadPoolExecutor.html) executor) Method that may be invoked by a [ThreadPoolExecutor](../../../../reference/java/util/concurrent/ThreadPoolExecutor.html) when [execute](../../../../reference/java/util/concurrent/ThreadPoolExecutor.html#execute(java.lang.Runnable)) cannot accept a task. |\n\nPublic Methods\n--------------\n\n#### public abstract void\n**rejectedExecution**\n([Runnable](../../../../reference/java/lang/Runnable.html) r, [ThreadPoolExecutor](../../../../reference/java/util/concurrent/ThreadPoolExecutor.html) executor)\n\nMethod that may be invoked by a [ThreadPoolExecutor](../../../../reference/java/util/concurrent/ThreadPoolExecutor.html) when\n[execute](../../../../reference/java/util/concurrent/ThreadPoolExecutor.html#execute(java.lang.Runnable)) cannot accept a\ntask. This may occur when no more threads or queue slots are\navailable because their bounds would be exceeded, or upon\nshutdown of the Executor.\n\nIn the absence of other alternatives, the method may throw\nan unchecked [RejectedExecutionException](../../../../reference/java/util/concurrent/RejectedExecutionException.html), which will be\npropagated to the caller of `execute`. \n\n##### Parameters\n\n| r | the runnable task requested to be executed |\n| executor | the executor attempting to execute this task |\n|----------|----------------------------------------------|\n\n##### Throws\n\n| [RejectedExecutionException](../../../../reference/java/util/concurrent/RejectedExecutionException.html) | if there is no remedy |\n|----------------------------------------------------------------------------------------------------------|-----------------------|"]]