-
Notifications
You must be signed in to change notification settings - Fork 1
/
ThreadCoordinationDaemon.java
46 lines (37 loc) · 1.59 KB
/
ThreadCoordinationDaemon.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
package com.jyotindersingh;
import java.math.BigInteger;
public class ThreadCoordinationDaemon {
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new LongComputationTask(new BigInteger("20000"), new BigInteger("1000000000")));
// In case the thread uses external code that does not handle
// thread interrupts, we can set the thread to be a daemon thread.
// Daemon threads don't block our application from exiting, when the main thread ends
// They can be used in usecases such as an autosave thread.
//
// So even if the entire calculation might not have finsihed, just the fact that the
// main thread ended, makes the entire app terminate.
thread.setDaemon(true);
thread.start();
Thread.sleep(100);
thread.interrupt();
}
private static class LongComputationTask implements Runnable {
private BigInteger base;
private BigInteger power;
public LongComputationTask(BigInteger base, BigInteger power) {
this.base = base;
this.power = power;
}
@Override
public void run() {
System.out.println(base + "^" + power + " = " + pow(base, power));
}
private BigInteger pow(BigInteger base, BigInteger power) {
BigInteger result = BigInteger.ONE;
for (BigInteger i = BigInteger.ZERO; i.compareTo(power) != 0; i = i.add(BigInteger.ONE)) {
result = result.multiply(base);
}
return result;
}
}
}