Skip to content

Commit 6215dce

Browse files
committed
Add Utils method to exit application after a timeout
1 parent 418a4a5 commit 6215dce

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.chrishantha.sample.base;
2+
3+
import java.util.Timer;
4+
import java.util.TimerTask;
5+
6+
/**
7+
* Some basic utilities for sample applications
8+
*/
9+
public class Utils {
10+
11+
/**
12+
* Exit application after a given number of seconds.
13+
*
14+
* @param timeout Timeout in seconds
15+
*/
16+
public static void exitApplication(int timeout) {
17+
if (timeout > 0) {
18+
final Timer timer = new Timer();
19+
timer.schedule(new TimerTask() {
20+
public void run() {
21+
System.out.println("Exiting now...");
22+
System.exit(0);
23+
}
24+
}, timeout * 1000);
25+
}
26+
}
27+
}

highcpu/src/main/java/com/github/chrishantha/sample/highcpu/HighCpuApplication.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717

1818
import com.beust.jcommander.Parameter;
1919
import com.github.chrishantha.sample.base.SampleApplication;
20-
21-
import java.util.Timer;
22-
import java.util.TimerTask;
20+
import com.github.chrishantha.sample.base.Utils;
2321

2422
public class HighCpuApplication implements SampleApplication {
2523

@@ -53,15 +51,7 @@ public class HighCpuApplication implements SampleApplication {
5351
@Override
5452
public void start() {
5553
System.out.println("Starting Application...");
56-
if (exitTimeoutInSeconds > 0) {
57-
final Timer timer = new Timer();
58-
timer.schedule(new TimerTask() {
59-
public void run() {
60-
System.out.println("Exiting now...");
61-
System.exit(0);
62-
}
63-
}, exitTimeoutInSeconds * 1000);
64-
}
54+
Utils.exitApplication(exitTimeoutInSeconds);
6555
if (runHashing) {
6656
for (int i = 0; i < hashingWorkers; i++) {
6757
startThread(i, "Hashing", new HashingWorker(hashDataLength, hashingAlgorithm));

0 commit comments

Comments
 (0)