Junior — Middle
Какой результат возвращает метод submit класса ThreadPoolExecutor?
sobes.tech AI
Answer from AI
The submit method of the ThreadPoolExecutor class in Java takes a task (usually a Runnable or Callable) and places it in the execution queue of the thread pool. It returns an object of type Future<V>, which represents the result of an asynchronous computation.
With this Future, you can:
- Check if the task has completed
- Get the execution result (if the task returns a value)
- Cancel the task
Example:
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<Integer> future = executor.submit(() -> {
// Some long-running operation
return 42;
});
// Get the result (blocks if the task is not yet finished)
Integer result = future.get();
System.out.println("Result: " + result);
executor.shutdown();