Use custom RejectedExecutionHandler

Use custom RejectedExecutionHandler

By : user2949273
Date : November 16 2020, 06:23 AM
With these it helps Yes it is possible to retry the execution of the rejected task. The best way to retry the execution is to use an alternate executor. You may declare a custom RejectedExecutionHandler class simply like as you do this with other interfaces. Here are some code samples:
code :
public class MyRejectedExecutionHandler implements RejectedExecutionHandler {

public void rejectedExecution(Runnable worker, ThreadPoolExecutor executor) {
    // TODO Auto-generated method stub
    System.out.println(worker.toString()+" is Rejected");

    System.out.println("Retrying to Execute");
        //Re-executing with alternateExecutor
        System.out.println(worker.toString()+" Execution Started");
    catch(Exception e)
        System.out.println("Failure to Re-exicute "+e.getMessage());


Share : facebook icon twitter icon
What is the runnable object passed in Java thread pool RejectedExecutionHandler

What is the runnable object passed in Java thread pool RejectedExecutionHandler

By : michaelluang
Date : March 29 2020, 07:55 AM
it helps some times The problem is when you submit task with submit method TreadPoolExecutor (actually AbstractExecutorService) wrap it to FutureTask. After that you receive FutureTask not your Runnable. You can call execute not submit:
code :
FutureTask myTask = (FutureTask) runnable;
Object result = myTask.get();
public MyFutureTask<V> extends FutureTask<V> {

    private Runnable myTask;

    public MyFutureTask(Runnable runnable, V result) {
        super(runnable, result);
        this.myTask = runnable;

    public Runnable getMyTask() {
        return myTask;
public class MyThreadPoolExecutor extends ThreadPoolExecutor {

    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
        return new MyFutureTask(task, value);            
MyFutureTask myFutureTask = (MyFutureTask) runnable;
MyTask myTask = (MyTask) myFutureTask.getMyTask();
// Now you can do what you want with you task:
Creating my custom security role and custom user group tables, to implement custom authorization for my asp.net mvc web

Creating my custom security role and custom user group tables, to implement custom authorization for my asp.net mvc web

By : SS2
Date : March 29 2020, 07:55 AM
I hope this helps . If the ability exists to maintain the active domain groups there is no reason to maintain a local groups table isinrole can be used for group access checks
Testing RejectedExecutionHandler in ThreadPoolExecutor

Testing RejectedExecutionHandler in ThreadPoolExecutor

By : Oscar Ali Castillo B
Date : March 29 2020, 07:55 AM
it fixes the issue I would personally create a situation where my ExecutorService will always reject a task and check that this task has been called using a counter.
So for example my code could be something like that:
code :
// A single threaded executor service that cannot have more than 1 task in its task queue
// such that I know that if I provide at least 3 tasks, at least 1 task will be rejected. 
// Why 3? 1 task in the queue + 1 task executed by the thread of the pool 
// = max of tasks that the pool can manage at a given time, so if I add 1 it will be
// rejected.
ExecutorService executor = new ThreadPoolExecutor(
    1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1),
    Executors.defaultThreadFactory(), myHandler

// My Counter
AtomicInteger counter = new AtomicInteger();
// Some arbitrary task that lasts long enough to make sure that at least 3
// tasks will be submitted that will increment my counter once completed
Runnable task = () -> {
    try {
    } catch (InterruptedException e) {
    } finally {
try {
    // Submit 3 tasks
} finally {
    // Shutdown the pool and wait until all the submitted tasks have been executed
    executor.awaitTermination(1L, TimeUnit.MINUTES);
// Ensure that we have 3 tasks that have been executed
assertEquals(3, counter.get());
Setting custom RejectedExecutionHandler for ThreadPoolExecutor after executing some tasks?

Setting custom RejectedExecutionHandler for ThreadPoolExecutor after executing some tasks?

By : Xinyu Wang
Date : March 29 2020, 07:55 AM
hope this fix your issue The question should be "Should we handle the RejectedExecutionException?". The answer is of course yes. Not doing so will crash the thread in which it occurs i.e. the thread submitting the task for execution, and the application will continue running in an unknown state.
The second question is "What should we do to handle this exception?". We should probably stop the application as cleanly as possible. As this is true also for other runtime exceptions (and errors, by the way), one solution is to use an UncaughtExceptionHandler. But if we want to handle the RejectedExecutionException in a specific manner, we may use a RejectedExecutionHandler. This could be used to do some specific handling before stopping the application (either directly or by throwing a new RuntimeException that will be caught by the UncaughtExceptionHandler).
RejectedExecutionHandler - CallerRunsPolicy vs AbortPolicy

RejectedExecutionHandler - CallerRunsPolicy vs AbortPolicy

By : Sari Rahmawati
Date : March 29 2020, 07:55 AM
may help you . I think you are already familiar with different RejectedExecutionHandlers of ThreadPoolExecutor
Related Posts Related Posts :
  • Multiple Consumers Spring Kafka
  • Bitbucket pipeline: environmental variables in build.gradle file not recognized
  • Java static enum method to return a default enum value
  • What is the complexity of empty for loop?
  • Group randomly List of Lists as N subsets with K elements in each
  • How to set texture on a shape drawn by glDrawArrays()?
  • How to make a jlabel resizable according to the window
  • Java Mathematical Expression Syntax
  • assigning values to characters in java
  • Method overriding and Inheritance in java
  • given a set of lists/groups with a series of number, find matching numbers
  • Connection to HID USB device (keyboard and mouse) in android
  • Inject HttpServletRequest in CDI SessionScoped bean
  • Pool game in java - ball collision algorithm
  • Focus ScrollView to selected position programmatically - Android
  • Is it hibernate bug?
  • Glib memory allocation error
  • Android Proguard - step by step
  • Setting the pivot point of a JPanel to its center
  • java check time is greater time
  • how to save a screenshot (matlab)
  • Anti-aliasing filled shapes in libgdx
  • Gwt custom text box having baloon popup
  • How to change date dynamically for each element in an Array
  • JPanel won't add the JLabel text?
  • how to access the .properties file in ant if it is in different location
  • Reduce application memory footprint
  • java.lang.ClassCastException: javax.mail.Session cannot be cast to javax.mail.Session
  • What is the exact purpose of calling System.exit() in java
  • How to do you get output from Javascript into Java using ScriptEngine
  • Java replaceAll() method to escape special characters
  • Java (Removing a 'keyword' from the alphabet)
  • Format BigDecimal in Spring
  • iterating checkbox values stored in array using EL
  • Java - Jackcess API with .accde(MS Access) format
  • Alternative for some of Struts 1 methods in Struts 2
  • javax.crypto JDK source code, again
  • Spring Roo - Command 'service' not found
  • antlr4: ATN version 2 expected 3
  • Deploy GlassFish using Netbeans generate java.net.MalformedURLException: Bad URL path
  • why is wait() called for loop in Thread.join() of Java?
  • Java JAXB marshall into DOM Document
  • Why JDBI 3 @ColumnName annotation doesn't work?
  • Java SimpleDateformatter with 10 decimals after the seconds, cannot convert to Date
  • Getting array index out of bounds when splitting a string in android
  • Verify if csv file is updated successfully
  • Get the parameter names of an interface method
  • How to retrieve object state from serialized Java objects without class file(s)
  • How to move a programmed game for pc to android
  • Size of an object and a string in Java
  • PrimeFaces Datatable Right Click Select + Row Specific Context Menu
  • How to use OOP to branch from a point class into lines and triangles (Java)
  • What does 'String...' mean?
  • Add YouTube Data API to Android Studio
  • JComboBox in JTable not displaying options, only the correct number of blank options
  • JTable and removeAll behavior
  • Google App Engine: Can I programmatically find out if this is the default version?
  • How do you open a program by using ActionListener in Java?
  • Android SipManager: android.net.sip.SipException: SipService.createSession() returns null
  • Forcefully terminating a thread I didn't write in Java
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org