Advertisement
Guest User

lab 8

a guest
Dec 6th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.concurrent.*;
  6.  
  7. public class Main {
  8.  
  9.     public static void main(String[] args) {
  10.  
  11.         double start = 0;
  12.         double end = Math.PI;
  13.         double dx = 0.001;
  14.         int threadNumber = 5;
  15.  
  16.  
  17.  
  18.         Calka_callable calka_callable = new Calka_callable(start,end,dx);
  19.         try {
  20.             calka_callable.call();
  21.         } catch (Exception e) {
  22.             e.printStackTrace();
  23.         }
  24.  
  25.         ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newFixedThreadPool(threadNumber);
  26.         List<Future<Double>> results = new ArrayList<>();
  27.  
  28.         double compartmentLength = (end - start)/threadNumber;
  29.         double lastPosition = compartmentLength*(threadNumber-1);
  30.  
  31.         for (int i = 0; i<threadNumber-1; i++){
  32.             Callable<Double> callable = new Calka_callable(i*compartmentLength, i*compartmentLength+compartmentLength,  dx);
  33.             Future<Double> result = executor.submit(callable);
  34.             results.add(result);
  35.         }
  36.  
  37.         Callable<Double> callable = new Calka_callable(lastPosition, lastPosition+compartmentLength,  dx);
  38.         Future<Double> result = executor.submit(callable);
  39.         results.add(result);
  40.  
  41.         double finalResult = 0;
  42.         for (Future<Double> future: results){
  43.             try {
  44.                 finalResult+=future.get();
  45.             } catch (InterruptedException e) {
  46.                 e.printStackTrace();
  47.             } catch (ExecutionException e) {
  48.                 e.printStackTrace();
  49.             }
  50.         }
  51.         System.out.println("Final result: "+finalResult);
  52.  
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement