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

  • SEARCH
  • Home
  • 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 8417533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:04:33+00:00 2026-06-10T02:04:33+00:00

If I have an ExecutorService to which I feed Runnable tasks, can I select

  • 0

If I have an ExecutorService to which I feed Runnable tasks, can I select one and interrupt it?
I know I can cancel the Future returned (also mentioned Here: how-to-interrupt-executors-thread), but how can I raise an InterruptedException. Cancel doesn’t seem to do it (event though it should by looking at the sources, maybe the OSX implementation differs). At least this snippet doesn’t print ‘it!’ Maybe I’m misunderstaning something and it’s not the custom runnable that gets the exception?

public class ITTest {
static class Sth {
    public void useless() throws InterruptedException {
            Thread.sleep(3000);
    }
}

static class Runner implements Runnable {
    Sth f;
    public Runner(Sth f) {
        super();
        this.f = f;
    }
    @Override
    public void run() {
        try {
            f.useless();
        } catch (InterruptedException e) {
            System.out.println("it!");
        }
    }
}
public static void main(String[] args) throws InterruptedException, ExecutionException {
    ExecutorService es = Executors.newCachedThreadPool();
    Sth f = new Sth();
    Future<?> lo = es.submit(new Runner(f));
    lo.cancel(true); 
    es.shutdown();
}

}

  • 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-10T02:04:35+00:00Added an answer on June 10, 2026 at 2:04 am

    The right thing to do here is to cancel the Future. The issue is that this will not necessarily cause an InterruptedException.

    If the job has yet to run then it will be removed from the runnable queue — I think this is your problem here. If the job has already finished then it won’t do anything (of course). If it is still running then it will interrupt the thread.

    Interrupting a thread will only cause sleep(), wait(), and some other methods to throw InterruptedException. You will also need test to see if the thread has been interrupted with:

    if (Thread.currentThread().isInterrupted()) {
    

    Also, it is a good pattern to re-set the interrupt flag if you catch InterruptedException:

    try {
       Thread.sleep(1000);
    } catch (InterruptedException e) {
       // this is a good pattern otherwise the interrupt bit is cleared by the catch
       Thread.currentThread().interrupt();
       ...
    }
    

    In your code, I would try putting a sleep before you call lo.cancel(true). It may be that you are canceling the future before it gets a chance to execute.

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

Sidebar

Related Questions

Suppose I have an ExecutorService (which can be a thread pool, so there's concurrency
Have deployed numerous report parts which reference the same view however one of them
Say I have a queue full of tasks which I need to submit to
I have a Tomcat webapp which does some pretty memory and CPU-intensive tasks on
I have a class containing an ExecutorService that can be shared between threads: class
I am using an unbounded, thread-caching Java-ExecutorService which concurrently processes my tasks. Now I
I have a class, myClass with this code in the main: ExecutorService service; List<Future<Integer>>
Say that I have the following code: ExecutorService executor = Executors.newSingleThreadExecutor(); executor.execute(myRunnable); Now, if
I currently have code that does the following: private final static ExecutorService pool =
I have two ExecutorServices, one to hold producers and the other one to hold

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.