1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
10 /** run cycle() repeatedly, no more often than minCycleTimeMillis; catch and log any exceptions. */
11 public abstract class ImmortalThread extends Thread {
13 private final int minCycleTimeMillis;
14 public ImmortalThread() { this(500); }
15 public ImmortalThread(int minCycleTimeMillis) { this.minCycleTimeMillis = minCycleTimeMillis; }
17 public abstract void cycle() throws Exception;
19 public final void run() {
21 long now = System.currentTimeMillis();
24 } catch (Exception e) { Log.warn(this, e); }
25 long now2 = System.currentTimeMillis();
26 if (now2 - now < minCycleTimeMillis)
27 try { Thread.sleep(minCycleTimeMillis - (now2 - now)); } catch (Exception e) { }