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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:40:33+00:00 2026-05-26T13:40:33+00:00

I have a fixed thread pool ExecutorService of width 10, and a list of

  • 0

I have a fixed thread pool ExecutorService of width 10, and a list of 100 Callable‘s, each waiting for 20 seconds and recording their interrupts.

I’m calling invokeAll on that list in a separate thread, and almost immediately interrupting this thread. ExecutorService execution is interrupted as expected, but the actual number of interrupts recorded by Callables is far more than expected 10 – around 20-40. Why is that so, if ExecutorService can execute no more than 10 threads simultaneously?

Full source: (You may need to run it more that once due to concurrency)

@Test
public void interrupt3() throws Exception{
    int callableNum = 100;
    int executorThreadNum = 10;
    final AtomicInteger interruptCounter = new AtomicInteger(0);
    final ExecutorService executorService = Executors.newFixedThreadPool(executorThreadNum);
    final List <Callable <Object>> executeds = new ArrayList <Callable <Object>>();
    for (int i = 0; i < callableNum; ++i) {
        executeds.add(new Waiter(interruptCounter));
    }
    Thread watcher = new Thread(new Runnable() {

        @Override
        public void run(){
            try {
                executorService.invokeAll(executeds);
            } catch(InterruptedException ex) {
                // NOOP
            }
        }
    });
    watcher.start();
    Thread.sleep(200);
    watcher.interrupt();
    Thread.sleep(200);
    assertEquals(10, interruptCounter.get());
}

// This class just waits for 20 seconds, recording it's interrupts
private class Waiter implements Callable <Object> {
    private AtomicInteger    interruptCounter;

    public Waiter(AtomicInteger interruptCounter){
        this.interruptCounter = interruptCounter;
    }

    @Override
    public Object call() throws Exception{
        try {
            Thread.sleep(20000);
        } catch(InterruptedException ex) {
            interruptCounter.getAndIncrement();
        }
        return null;
    }
}

Using WinXP 32-bit, Oracle JRE 1.6.0_27 and JUnit4

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

    I disagree with the hypothesis that you should only receive 10 interrupts.

    Assume the CPU has 1 core.
    1. Main thread starts Watcher and sleeps
    2. Watcher starts and adds 100 Waiters then blocks
    3. Waiter 1-10 start and sleep in sequence
    4. Main wakes and interrupts Watcher then sleeps
    5. Watcher cancels Waiter 1-5 then is yielded by the OS   (now we have 5 interrupts)
    6. Waiter 11-13 start and sleep
    7. Watcher cancels Waiter 6-20 then is yielded by the OS   (now we have 13 interrupts)
    8. Waiter 14-20 are "started" resulting in a no-op
    9. Waiter 21-24 start and sleep
    ....
    

    Essentially, my argument is that there is no guarantee that the Watcher thread will be allowed to cancel all 100 “Waiter” RunnableFuture instances before it has to yield the time slice and allow the ExecutorService’s worker threads to start more Waiter tasks.

    Update: Showing code from AbstractExecutorService

    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
        throws InterruptedException {
        if (tasks == null)
            throw new NullPointerException();
        List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
        boolean done = false;
        try {
            for (Callable<T> t : tasks) {
                RunnableFuture<T> f = newTaskFor(t);
                futures.add(f);
                execute(f);
            }
            for (Future<T> f : futures) {
                if (!f.isDone()) {
                    try {
                        f.get(); //If interrupted, this is where the InterruptedException will be thrown from
                    } catch (CancellationException ignore) {
                    } catch (ExecutionException ignore) {
                    }
                }
            }
            done = true;
            return futures;
        } finally {
            if (!done)
                for (Future<T> f : futures)
                    f.cancel(true); //Specifying "true" is what allows an interrupt to be sent to the ExecutorService's worker threads
        }
    }
    

    The finally block which contains f.cancel(true) is when the interrupt would be propagated to task which is currently running. As you can see, this is a tight loop, but there is no guarantee that the thread executing the loop would be able to iterate through all instances of Future in one time slice.

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

Sidebar

Related Questions

I have a fixed thread pool that I submit tasks to (limited to 5
I have a Java application that has a fixed thread pool of fifteen, the
I have a fixed thread pool that runs 7 concurrent threads at any time
I have a fixed width DIV containing a table with many columns, and need
I'm allocating my pthread thread-specific data from a fixed-size global pool that's controlled by
I have mainly CPU intensive operation which is running on a thread pool. Operation
I have a situation where I expect adding to the thread pool to be
EDIT: I have fixed all but two warnings now, so thank you all for
I can't make td Date to have fixed height. If there is less in
Header, footer and sidebars have fixed position. In the center a content area with

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.