Java executor: how do I stop a submitted task?

I have submitted a task using the actuator and I need it to stop after a period of time (e.g. 5 minutes) I've tried 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 task and then read by a thread. Even if I stop all tasks, if the timeout occurs, I get:

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

What's wrong? How to stop the submitted work after 5 minutes?

Solution

Just because you call cancel () on future does not mean that the task will stop automatically You must do something in the task to make sure it stops:

>Use cancel (true) to send the interrupt to the task. > Handle interruptedexception If a function in your task throws an interruptedexception, make sure to exit as soon as possible when an exception is caught. > If the task performs continuous calculations, periodically check the thread currentThread(). isInterrupted().

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

In addition, as mentioned in other posts, even if the thread safe vector class is used, the concurrentmodificationexception can be thrown because the iterator obtained from the vector is not thread safe and needs to be synchronized Advanced for loops use iterators, so note:

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);
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>