/
n1tron
/
practice07
Обзор
Документация
Войти
/
n1tron
/
practice07
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/org/example/MainExecutorService.java
54 строки
2 KB
RodinRoman
init
23 май 2025, 02:17
23 май 2025, 02:17
4f61476
Код
Авторство
О чём код?
package org.example; import java.util.concurrent.*; public class MainExecutorService { public static void main(String[] args) throws ExecutionException, InterruptedException { ExecutorService executorService = Executors.newFixedThreadPool(10); Callable<Integer> task = () -> { long id = Thread.currentThread().getId(); System.out.println("Callable thread class id = " + id + " started"); int sum = 0; for (int i = 0; i < 5; ++i) { sum += i; } System.out.println("Callable thread class id = " + id + " finished"); return sum; }; Future<Integer> future = executorService.submit(task); System.out.println("result: " + future.get()); Integer a = future.get(); ++a; System.out.println("Main thread class id = " + Thread.currentThread().getId() + " finished"); CompletableFuture<String> welcomeText = CompletableFuture.supplyAsync(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { throw new IllegalStateException(e); } System.out.println(Thread.currentThread().getName()); return "Rajeev"; }).thenApply(name -> { System.out.println(Thread.currentThread().getName()); return "Hello " + name; }).thenApply(greeting -> { System.out.println(Thread.currentThread().getName()); return greeting + ", Welcome to the CalliCoder Blog"; }); System.out.println(Thread.currentThread().getName()+" go further"); // Block and wait for the future to complete System.out.println(Thread.currentThread().getName()+" "+welcomeText .get()); executorService.shutdown(); } }