Я играл с различными ExecutorServices, чтобы проверить их способ выполнения задач, которые я представил.Приведенный ниже код демонстрирует, какие сервисы я тестировал и как они получают свои задачи для выполнения.
В основном я хотел бы знать, почему WorkStealingPool засыпает (см. Результаты ниже), а CachedThreadPool работает как задумано?
void start() {
ExecutorService service;
// The different services I tried:
service = Executors.newFixedThreadPool(1);
// service = Executors.newCachedThreadPool();
// service = Executors.newWorkStealingPool();
Runnable r = () -> {
Callable<Integer> result = () -> {
Future<Integer> future = service.submit(() -> 1 + 1);
return service.submit(() -> (future.get() / 2)).get();
};
};
// Things I tried:
// 1. Run via separate Thread:
// Thread t = new Thread(r);
// t.start();
// 2. Submit to service:
service.submit(r);
// 3. Run directly:
// r.run();
}
Мои текущие результаты тестирования:
service executiontype behavior
1 1 1
2 1 2
3 1 3
1 2 1
2 2 2
3 2 3
1 3 1
2 3 4
3 3 5
service
1 FixedThreadPool
2 CachedThreadPool
3 WorkStealingThreadPool
executiontype
1 separate thread
2 submit to service
3 run directly
behavior
1 Short activity on one thread, GUI not responding
2 Everything executes as expected, GUI thread not blocked
3 Threads are being active but some go into wait and remain waiting.
GUI has to be shutdown by force.
Multiple calculation invokations deliver the result sometimes.
4 Like #3 but with blocked/waiting GUI thread
5 Like #2 but GUI thread blocked (as expected)