1package jp.co.whitebase.web;
2
3import java.util.concurrent.TimeUnit;
4import java.util.concurrent.atomic.AtomicLong;
5
6// For websites that keep on running
7public class UptimeOptimizer {
8
9 static final AtomicLong uptime =
10 new AtomicLong(0);
11 static final long FOREVER = Long.MAX_VALUE;
12
13 public static void main(String[] args)
14 throws Exception {
15
16 System.out.println("Keeping on running...");
17
18 while (uptime.get() < FOREVER) {
19 keepRunning();
20 uptime.incrementAndGet();
21 TimeUnit.SECONDS.sleep(1);
22 }
23 // This line is never reached
24 System.out.println("Done");
25 }
26
27 static void keepRunning()
28 throws Exception {
29
30 long t = uptime.get();
31
32 if (t % 1_000_000 == 0) {
33 // Report to all every 1 million seconds
34 MailService.sendToAll(
35 "Still running strong");
36 }
37
38 if (t == 292_471_208_677L) {
39 // long limit ≈ 9,263 years from now
40 System.out.println(
41 "That's long enough");
42 System.exit(0);
43 }
44 }
45}
46// javac *.java && java UptimeOptimizer