Вопрос

I'd like to stop an Executor from running any more Future objects even if they have been submitted to the Executor. Getting multiple threads running though an Executor all works fine and well but the Executor should stop when one of the Callable's returns a Boolean TRUE. It's fine that the current running Future's complete but it would be a waste of time to continue the rest.

Set<Future<Boolean>> calculationSet = new HashSet<Future<Boolean>>();
threadPool = Executors.newFixedThreadPool(3);

int x = nextX();
int y = nextY();

do {
   Callable<Boolean> mathCalculation = new MathCalculation(x, y);
   Future<Boolean> futureAttempt = threadPool.submit(mathCalculation);
   calculationSet.add(futureAttempt);

   x = nextX();
   y = nextY();
} while (runSomeMore());

Where MathCalculation implements the Callable interface and its call() method returns a Boolean value.

Searching through the calculationSet on each iteration isn't an option since the set will become quite large and it will obviously not work because of the multi threaded situation. Is there a way to achieve this?

Нет правильного решения

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top