Advertisement
sergAccount

Untitled

Jan 30th, 2021
710
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 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.app4;
  7.  
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. /**
  12.  *
  13.  * @author Admin
  14.  */
  15. public class Main {
  16.    
  17.     public static void main(String[] args) {
  18.         //
  19.         int[] array = {1, 2, 3, 4};
  20.         AvgTask a1 = new AvgTask(array);
  21.         AvgTask a2 = new AvgTask(array);
  22.         // массив потоков
  23.         Thread[] arr = {new Thread(a1), new Thread(a2)};
  24.         // запускаем все потоки на выполнение
  25.         for (int i = 0; i < arr.length; i++) {
  26.             arr[i].start();            
  27.         }
  28.         // ожидаем завершения выполнения каждого из потоков
  29.         for (int i = 0; i < arr.length; i++) {
  30.             try {
  31.                 arr[i].join();
  32.             } catch (InterruptedException ex) {
  33.                 ex.printStackTrace();
  34.             }
  35.         }
  36.         // поучаем рез-ты всех задач
  37.         System.out.println("a1.getResult()=" + a1.getResult());
  38.         System.out.println("a2.getResult()=" + a2.getResult());
  39.         double total = a1.getResult() * a2.getResult();
  40.         System.out.println("total=" + total);
  41.     }
  42. }
  43.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement