Skip to content

Instantly share code, notes, and snippets.

@Shtaba09
Created December 6, 2018 23:36
Show Gist options
  • Save Shtaba09/9e587b54728e5228047af2495eddadfa to your computer and use it in GitHub Desktop.
Save Shtaba09/9e587b54728e5228047af2495eddadfa to your computer and use it in GitHub Desktop.
Gaus формула и BigDeclima Просчет суммы чисел от входа
package com.javarush.task.task28.task2808;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;
/*
Осваиваем Callable
*/
public class Solution {
public static void main(String[] args) throws InterruptedException, ExecutionException {
List<Future<String>> futures = new ArrayList<>();
ExecutorService executor = Executors.newFixedThreadPool(5);
for (int i = 1000; i <= 1010; i++) {
futures.add(executor.submit(getTask(i)));
}
futures.add(executor.submit(getTask(10000000)));
for(Future<String> future : futures) {
System.out.println(future.get());
}
executor.shutdown();
executor.awaitTermination(10, TimeUnit.SECONDS);
/* output
500500
501501
502503
503506
504510
505515
506521
507528
508536
509545
510555
50000005000000
*/
}
public static Callable<String> getTask(final int i) {
/* Callable<String> callable = new Callable<String>() {
@Override
public String call() throws Exception {
BigDecimal s= new BigDecimal("0");
for (int j=1; j<=i; j++){
s = s.add(BigDecimal.valueOf(j));
}
return s.toString();
}
};
return callable;*/
return () -> ((long)i*(i+1))/2+"";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment