Advertisement
sergAccount

Untitled

Jan 30th, 2021
742
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.37 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 AvgTask implements Runnable {
  16.  
  17.     private int[] array;
  18.     private double result;
  19.  
  20.     public AvgTask(int[] array) {
  21.         this.array = array;
  22.     }
  23.     // метод для получения результата
  24.     public double getResult() {
  25.         return result;
  26.     }
  27.     @Override
  28.     public void run() {
  29.         //
  30.         System.out.println("AvgTask.run=" + Thread.currentThread().getName() + ">>");
  31.        
  32.         long startTime = System.currentTimeMillis(); //System.nanoTime()
  33.         //
  34.         for (int i = 0; i < array.length; i++) {
  35.             result += array[i];
  36.         }
  37.         result = result / array.length;
  38.         try {
  39.             Thread.sleep(20);
  40.         } catch (InterruptedException ex) {
  41.             ex.printStackTrace();
  42.         }
  43.         // получаем время выполнения задачи в ms
  44.         long totalTime = System.currentTimeMillis() - startTime;
  45.         System.out.println("AvgTask.totalTime=" + totalTime);
  46.         //
  47.         System.out.println("<<");
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement