Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package com.mycompany.app5;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.Future;
- /**
- *
- * @author Admin
- */
- public class Main3 {
- public static void main(String[] args) {
- try {
- ExecutorService executor = Executors.newFixedThreadPool(5);
- // ...
- Callable<String> c1 = () -> {
- System.out.println("Task c1!");
- return "res1";
- };
- Callable<String> c2 = () -> {
- System.out.println("Task c2!");
- return "res2";
- };
- // Запускаем задачи на выполнение в асинк режиме и ждем результата выполнения всех задач
- List<Callable<String>> list = new ArrayList();
- list.add(c1);
- list.add(c2);
- // invokeAll - запускаем все задачи на выполение и ждем завершения всех задач
- List<Future<String>> results = executor.invokeAll(list);
- for(Future<String> f: results){
- String res1 = f.get();
- System.out.println("Future.res=" + res1);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement