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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T12:15:18+00:00 2026-05-12T12:15:18+00:00

I have submitted a task using executors and I need it to stop after

  • 0

I have submitted a task using executors and I need it to stop after some time (e.g. 5 minutes). I have tried doing like this:

   for (Future<?> fut : e.invokeAll(tasks, 300, TimeUnit.SECONDS)) {
         try {
             fut.get(); 
         } catch (CancellationException ex) {
             fut.cancel(true);   
             tasks.clear();
         } catch(ExecutionException ex){
             ex.printStackTrace(); //FIXME: gestita con printstack       
         }
   }

But I always get an error: I have a shared Vector that needs to be modified by the tasks and then read by a thread, and even if I stop all the task, if the timeout occurs I get:

Exception in thread "Thread-1" java.util.ConcurrentModificationException

Is there something wrong? How can I stop the tasks submitted that are still working after 5 minutes?

  • 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-12T12:15:18+00:00Added an answer on May 12, 2026 at 12:15 pm

    Just because you call cancel() on Future doesn’t mean that the task will stop automatically. You have to do some work within the task to make sure that it will stop:

    • Use cancel(true) so that an interrupt is sent to the task.
    • Handle InterruptedException. If a function in your task throws an InterruptedException, make sure you exit gracefully as soon as possible upon catching the exception.
    • Periodically check Thread.currentThread().isInterrupted() if the task does continuous computation.

    For example:

    class LongTask implements Callable<Double> {
        public Double call() {
            
             // Sleep for a while; handle InterruptedException appropriately
             try {
                 Thread.sleep(10000);
             } catch (InterruptedException ex) {
                 System.out.println("Exiting gracefully!");
                 return null;
             }
    
    
             // Compute for a while; check Thread.isInterrupted() periodically
             double sum = 0.0;
             for (long i = 0; i < 10000000; i++) {
                 sum += 10.0
                 if (Thread.currentThread().isInterrupted()) {
                     System.out.println("Exiting gracefully");
                     return null;
                 }
             }
    
             return sum;
        } 
    }
    

    Also, as other posts have mentioned: ConcurrentModificationException can be thrown even if using the thread-safe Vector class, because iterators you obtain from Vector are not thread-safe, and thus need to be synchronized. The enhanced for-loop uses iterators, so watch out:

    final Vector<Double> vector = new Vector<Double>();
    vector.add(1.0);
    vector.add(2.0);
    
    // Not thread safe!  If another thread modifies "vector" during the loop, then
    // a ConcurrentModificationException will be thrown.
    for (Double num : vector) {
        System.out.println(num);
    }
    
    // You can try this as a quick fix, but it might not be what you want:
    synchronized (vector) {    // "vector" must be final
        for (Double num : vector) {
            System.out.println(num);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 195k
  • Answers 195k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use: AND oh.tran_date BETWEEN TRUNC(SYSDATE - 1) AND TRUNC(SYSDATE) -… May 12, 2026 at 6:54 pm
  • Editorial Team
    Editorial Team added an answer I have been using FluentValidation along with jQuery validation plugin… May 12, 2026 at 6:54 pm
  • Editorial Team
    Editorial Team added an answer Your problem is here: var selectNode = function(node) { node.select();… May 12, 2026 at 6:54 pm

Related Questions

I have a form in which the user selects a few items to display
I would like to implement a thread pool in Java, which can dynamically resize
My colleagues are attempting to connect BizTalk 2006 R2 via DB2/MVS adapter to a
I've created a custom workflow which creates a task item when the workflow is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.