Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8047483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T06:06:47+00:00 2026-06-05T06:06:47+00:00

I have thread pool with a RejectedExecutionHandler like this and a Runnable task. sExecutorService

  • 0

I have thread pool with a RejectedExecutionHandler like this and a Runnable task.

sExecutorService = new ThreadPoolExecutor( ...    new MyRejectionHandler() );

interface MyTask extends Runnable { ... }

class MyTaskImpl implements MyTask { ...}     

i execute a task like this

   sExecutorService.submit(myTask);        

in rejectedExecution case i was hoping to get hold of rejected Runnable (ie MyTask) and set some field in it marking it as rejected. But i am not able to cast it to MyTask.
So what exactly is the runnable passed t o rejectedExecution ? It appears not MyTask i submitted . And how can i get hold of the rejected task in RejectedExecutionHandler.

public class MyRejectionHandler implements RejectedExecutionHandler{  
   public void rejectedExecution(Runnable runnable, ThreadPoolExecutor executor)  {
    MyTask myTask = (MyTask) runnable; // exception
    myTask.doSomething();
   }
java.lang.ClassCastException: java.util.concurrent.FutureTask cannot be cast to MyTask       
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T06:06:50+00:00Added an answer on June 5, 2026 at 6:06 am

    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:

    sExecutorService.execute(yourTask);
    

    I don’t think there is a way to get your task from FutureTask. You can only call run for it. So if you want to call submit and you need to call run – just do not convert to MyTask:

    FutureTask myTask = (FutureTask) runnable;
    myTask.run();
    Object result = myTask.get();
    

    Other way if you want to access you MyTask object can be creating of MyFutureTask extends FutureTask, that will allow get your object:

    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;
        }        
    }
    

    Also you need to extends ThreadPoolExecutor and redefine newTaskFor method that is responsible for Runnable to FutureTask wraping:

    public class MyThreadPoolExecutor extends ThreadPoolExecutor {
    
        @Override
        protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {
            return new MyFutureTask(task, value);            
        }
    }
    

    After that you can use MyThreadPoolExecutor as a ThreadPoolExecutor and in rejecting task processing:

    MyFutureTask myFutureTask = (MyFutureTask) runnable;
    MyTask myTask = (MyTask) myFutureTask.getMyTask();
    // Now you can do what you want with you task:
    myTask.doSomthing();    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a thread pool (executor) which I would like to monitor for excessive
I have used Thread Pool for New IO server design . I have used
What I have is something similar to this: //method being called by thread pool
If I have a common thread pool that queues runnable tasks and the runnable
I have an existing java/scala application using a global thread pool. I would like
I have a WorkManager+ThreadPool under Glassfish that uses TIMED_WAIT, such as this: p: thread-pool-1;
Let's say I have a thread pool containing X items, and a given task
I have a thread pool created using java.util.concurrent.ThreadPoolExecutor Is there anyway I can wait
I have a thread pool with threads doing specific tasks. After the task is
I have a custom thread pool class, that creates some threads that each wait

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.