Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.concurrent.Callable;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ExecutionException;
- import java.util.concurrent.Future;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.concurrent.*;
- public class Calka_callable implements Callable<Double>
- {
- private double pocz;
- private double kon;
- private double dx;
- private double xp;
- private double xk;
- private int N;
- public static final int watki = 1;
- public Calka_callable(double xp, double xk, double dx)
- {
- this.xp = xp;
- this.xk = xk;
- this.pocz = xp;
- this.kon = xk;
- this.N = (int) Math.ceil((xk-xp)/dx);
- this.dx = (xk-xp)/N;
- System.out.println("Creating an instance of Calka_callable");
- System.out.println("xp = " + xp + ", xk = " + xk + ", N = " + N);
- System.out.println("dx requested = " + dx + ", dx final = " + this.dx);
- }
- private double getFunction(double x)
- {
- return Math.sin(x);
- }
- public double compute()
- {
- double calka = 0;
- int i;
- for(i=0; i<N; i++)
- {
- double x1 = xp+i*dx;
- double x2 = x1+dx;
- calka += ((getFunction(x1) + getFunction(x2))/2.)*dx;
- }
- return calka;
- }
- @Override
- public Double call() throws Exception
- {
- double calka = compute();
- System.out.println("Calka czastkowa: w przedziale : ["+pocz+"; "+kon+"] = " + calka);
- return calka;
- }
- public static void main(String[] args)
- {
- ExecutorService executor = Executors.newFixedThreadPool(watki);
- List<Future<Double>> list = new ArrayList<>();
- double suma = 0.0, poczatek = 0.0, koniec = 5, l_zadan = 16;
- double start, stop;
- for(int i = 0; i < l_zadan; i++)
- {
- double x = ((koniec - poczatek) / l_zadan);
- start = i * x;
- stop = start + x;
- Callable<Double> callable = new Calka_callable(start, stop,0.0001);
- Future<Double> future = executor.submit(callable);
- list.add(future);
- }
- for(Future<Double> el:list)
- {
- try
- {
- suma = suma + el.get();
- }
- catch(InterruptedException | ExecutionException e)
- {
- e.printStackTrace();
- }
- }
- //executorService.shutdown();
- //while(!executorService.isTerminated()){
- //
- //}
- System.out.println("\nCalka = "+suma);
- executor.shutdown();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement