-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactorialThread.java
69 lines (51 loc) · 1.49 KB
/
FactorialThread.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
56
57
58
59
60
61
62
63
64
65
66
67
68
package threadLesson;
import java.math.BigInteger;
public class FactorialThread implements Runnable {
private String name;
private BigInteger factorial;
public FactorialThread() {
super();
}
private BigInteger factorial() {
BigInteger fact = new BigInteger("1");
BigInteger num = new BigInteger(
(Thread.currentThread().getName().substring(Thread.currentThread().getName().lastIndexOf("-") + 1)));
for (int i = 1; i <= num.intValue(); i += 1) {
fact = fact.multiply(new BigInteger(integerToString(i)));
}
return fact;
}
private String integerToString(int n) {
String res = "";
try {
res = Integer.toString(n);
} catch (NumberFormatException e) {
e.printStackTrace();
}
return res;
}
private void showFactorial(BigInteger f) {
this.setFactorial(f);
this.setName(Thread.currentThread().getName());
}
@Override
public void run() {
showFactorial(factorial());
}
public BigInteger getFactorial() {
return factorial;
}
private void setFactorial(BigInteger factorial) {
this.factorial = factorial;
}
public String getName() {
return name;
}
private void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "FactorialThread [name = " + name + ", factorial = " + factorial + "]";
}
}