Advertisement
sergAccount

Untitled

Feb 6th, 2021
610
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.53 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package com.mycompany.app8_dz;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import java.util.concurrent.ExecutorService;
  11. import java.util.concurrent.Executors;
  12. import java.util.concurrent.Future;
  13.  
  14. /**
  15.  *
  16.  * @author Admin
  17.  */
  18. public class Main2 {    
  19.     //
  20.     public static void main(String[] args) {
  21.         //создаем пул потоков
  22.         ExecutorService service = Executors.newFixedThreadPool(5);
  23.         //
  24.         int[] array1 = {6, 12, 48, 32};
  25.         int[] array2 = {6, 12, 48, 32};
  26.         AvgTask avg1 = new AvgTask(array1);
  27.         AvgTask avg2 = new AvgTask(array2);
  28.         // создаем коллекцию задач
  29.         ArrayList<AvgTask> list = new ArrayList<>();
  30.         list.add(avg1);
  31.         list.add(avg2);        
  32.         try{
  33.             double total = 1;
  34.             // invokeAll - для запуска задач с помощью пула потока (ExecutorService)
  35.             List<Future<Double>> fList = service.invokeAll(list);
  36.             for(Future<Double> f: fList){
  37.                 Double value = f.get();
  38.                 System.out.println("AvgTask.value=" + value);
  39.                 total *= value;  
  40.             }            
  41.             System.out.println("total=" + total);
  42.         }catch(Exception exc){
  43.             exc.printStackTrace();
  44.         }
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement