Skip to content

Commit 0ffa1bf

Browse files
authored
carried m away in spirit 2 great & high mountain
And he carried me away in the spirit to a great and high mountain, and shewed me that great city, the holy Jerusalem, descending out of heaven from God (Revelation 21:10)
1 parent 74235c2 commit 0ffa1bf

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
//And he carried me away in the spirit to a great and high mountain, and shewed me that great city, the holy Jerusalem, descending out of heaven from God (Revelation 21:10)
3+
4+
package com.javarush.task.task28.task2808;
5+
6+
7+
import java.math.BigDecimal;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.concurrent.*;
11+
12+
/*
13+
Осваиваем Callable
14+
*/
15+
public class Solution {
16+
public static void main(String[] args) throws InterruptedException, ExecutionException {
17+
List<Future<String>> futures = new ArrayList<>();
18+
ExecutorService executor = Executors.newFixedThreadPool(5);
19+
for (int i = 1000; i <= 1010; i++) {
20+
futures.add(executor.submit(getTask(i)));
21+
}
22+
23+
futures.add(executor.submit(getTask(10000000)));
24+
25+
for(Future<String> future : futures) {
26+
System.out.println(future.get());
27+
}
28+
29+
executor.shutdown();
30+
executor.awaitTermination(10, TimeUnit.SECONDS);
31+
32+
/* output
33+
500500
34+
501501
35+
502503
36+
503506
37+
504510
38+
505515
39+
506521
40+
507528
41+
508536
42+
509545
43+
510555
44+
50000005000000
45+
*/
46+
}
47+
48+
public static Callable<String> getTask(final int i) {
49+
return new Callable<String>() {
50+
private BigDecimal summ = new BigDecimal(0);
51+
52+
@Override
53+
public String call() throws Exception {
54+
for (int j = 1; j < i+1; j++) {
55+
summ = summ.add(BigDecimal.valueOf(j));
56+
}
57+
return summ.toString();
58+
}
59+
};
60+
}
61+
}
62+
63+
/*
64+
Осваиваем Callable
65+
66+
Реализуй логику метода getTask, который должен возвращать объект Callable.
67+
68+
Объект Callable должен вычислять сумму всех чисел от 1 до переданного числа i включая его, и возвращать его в виде строки.
69+
70+
Метод main не участвует в тестировании.
71+
72+
73+
74+
75+
76+
Требования:
77+
78+
1. Класс Solution должен содержать статический метод getTask(int).
79+
80+
2. Метод getTask должен возвращать объект Callable.
81+
82+
3. Объект Callable должен возвращать в виде строки сумму всех чисел от 1 до переданного числа i, включая его
83+
84+
4. Сумма должна правильно считаться для чисел произвольной длины.
85+
*/

0 commit comments

Comments
 (0)