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 6097569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:03:32+00:00 2026-05-23T13:03:32+00:00

public void execute(Runnable command) This command object contains the submitted object, but it seems

  • 0

public void execute(Runnable command)

This command object contains the submitted object, but it seems to have been wrapped.

How can I access the submitted object from within a custom thread pool executor? Or is it not such a good idea to try and access the submitted object from inside a ThreadPoolExecutor’s before/after/execute methods?

  • 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-05-23T13:03:33+00:00Added an answer on May 23, 2026 at 1:03 pm

    Don’t use execute, use submit, which returns a Future, which is a handle to the task. Here’s some example code:

    ExecutorService service = Executors.newCachedThreadPool();
    
    Callable<String> task = new Callable<String>() {
        public String call() throws Exception {
            return "hello world";
        }
    };
    
    Future<String> future = service.submit(task);
    

    Although you can’t access the task directly, you can still interact with it:

    future.cancel(); // Won't start task if not already started
    String result = future.get(); // blocks until thread has finished calling task.call() and returns result
    future.isDone(); // true if complete
    

    You can also interact with the service:

    service.shutdown(); //etc    
    

    EDITED TO INCORPORATE COMMENTS:

    If you want to do some logging, use an anonymous class to override the afterExecute() method, like this:

        ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)) {
            @Override
            protected void afterExecute(Runnable r, Throwable t)
            {
                // Do some logging here
                super.afterExecute(r, t);
            }
        };
    

    Override other methods as required.

    Quick plug: IMHO, the bible for this subject is Java Concurrency in Practice – I recommend you buy it and read it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Look ath this method of ThreadPoolExcecutor: public void execute(Runnable command) { ... if (runState
I have an interface method public void Execute(ICommand command); which needs to pass known
So far I've been using public void run() {} methods to execute my code
My code looks like this, this is Server code ( Runnable ) public void
I have this class: public class Test { public static void main(String[] args) {
I have this code : public void GenerateWtW() { ExecutorService exec = Executors.newFixedThreadPool(30); ConcurrentHashMap<String,
I have a method which uses CyclicBarrier as shown below: public void getMessage(Message obj){
I have a thread running but from outside I can't bypass a value to
I have the following sample piece of code, public class test implements Runnable{ public
public void test() { List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); for (int

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.