-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayMultiThreadCalc.java
55 lines (45 loc) · 1.54 KB
/
ArrayMultiThreadCalc.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package threadLesson;
import java.math.BigInteger;
import java.util.List;
public class ArrayMultiThreadCalc implements Runnable{
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_GREEN = "\u001B[32m";
private List<Integer> array;
private BigInteger result;
public ArrayMultiThreadCalc(List<Integer> array) {
this.array = array;
}
public ArrayMultiThreadCalc() {
}
public List<Integer> getArray() {
return array;
}
public void setArray(List <Integer> array) {
this.array = array;
}
public BigInteger getResult() {
return result;
}
public BigInteger calculationOfArrElementsSum(List <Integer> array){
BigInteger result = BigInteger.ZERO;
for (int i = 0; i < array.size(); i++){
result = result.add(new BigInteger(String.valueOf(array.get(i))));
}
return result;
}
@Override
public void run() {
Thread thread = Thread.currentThread();
ArrayMultiThreadCalc arrayCalc = new ArrayMultiThreadCalc(array);
result = arrayCalc.calculationOfArrElementsSum(array);
System.out.println("The sum of array elements from thread with ID: " + ANSI_GREEN + thread.getId() + ANSI_RESET
+ " and name: " + ANSI_GREEN + thread.getName() + ANSI_RESET + " is: " + result);
}
@Override
public String toString() {
return "ArrayMultiThreadCalc{" +
"array=" + array +
", result=" + result +
'}';
}
}