Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. import java.util.concurrent.Callable;
  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.ExecutionException;
  5. import java.util.concurrent.Future;
  6. import java.util.List;
  7. import java.util.ArrayList;
  8. import java.util.concurrent.*;
  9.  
  10. public class Calka_callable implements Callable<Double>
  11. {
  12.     private double pocz;
  13.     private double kon;
  14.     private double dx;
  15.     private double xp;
  16.     private double xk;
  17.     private int N;
  18.     public static final int watki = 1;
  19.  
  20.     public Calka_callable(double xp, double xk, double dx)
  21.     {
  22.         this.xp = xp;
  23.         this.xk = xk;
  24.         this.pocz = xp;
  25.         this.kon = xk;
  26.         this.N = (int) Math.ceil((xk-xp)/dx);
  27.         this.dx = (xk-xp)/N;
  28.         System.out.println("Creating an instance of Calka_callable");
  29.         System.out.println("xp = " + xp + ", xk = " + xk + ", N = " + N);
  30.         System.out.println("dx requested = " + dx + ", dx final = " + this.dx);
  31.     }
  32.  
  33.     private double getFunction(double x)
  34.     {
  35.         return Math.sin(x);
  36.     }
  37.  
  38.     public double compute()
  39.     {
  40.         double calka = 0;
  41.         int i;
  42.         for(i=0; i<N; i++)
  43.         {
  44.             double x1 = xp+i*dx;
  45.             double x2 = x1+dx;
  46.             calka += ((getFunction(x1) + getFunction(x2))/2.)*dx;
  47.         }
  48.         return calka;
  49.     }
  50.  
  51.     @Override
  52.     public Double call() throws Exception
  53.     {
  54.         double calka = compute();
  55.         System.out.println("Calka czastkowa: w przedziale : ["+pocz+"; "+kon+"] = " + calka);
  56.         return calka;
  57.     }
  58.  
  59.     public static void main(String[] args)
  60.     {
  61.         ExecutorService executor = Executors.newFixedThreadPool(watki);
  62.         List<Future<Double>> list = new ArrayList<>();
  63.  
  64.         double suma = 0.0, poczatek = 0.0, koniec = 5, l_zadan = 16;
  65.         double start, stop;
  66.  
  67.         for(int i = 0; i < l_zadan; i++)
  68.         {
  69.             double x = ((koniec - poczatek) / l_zadan);
  70.             start = i * x;
  71.             stop = start + x;
  72.             Callable<Double> callable = new Calka_callable(start, stop,0.0001);
  73.             Future<Double> future = executor.submit(callable);
  74.             list.add(future);
  75.         }
  76.         for(Future<Double> el:list)
  77.         {
  78.             try
  79.             {
  80.                 suma = suma + el.get();
  81.             }
  82.             catch(InterruptedException | ExecutionException e)
  83.             {
  84.                 e.printStackTrace();
  85.             }
  86.  
  87.         }
  88.  
  89.         //executorService.shutdown();
  90.         //while(!executorService.isTerminated()){
  91.         //
  92.         //}
  93.         System.out.println("\nCalka = "+suma);
  94.         executor.shutdown();
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement