X-Git-Url: http://git.megacz.com/?p=org.ibex.util.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Futil%2FImmortalThread.java;fp=src%2Forg%2Fibex%2Futil%2FImmortalThread.java;h=90c512593c06ea64203f4996e69358759503a021;hp=0000000000000000000000000000000000000000;hb=9e272ba8f5ef575de39c70bf8c3709977cb739e1;hpb=ded3134be3462ef377baa59fad9267e93c2232cb diff --git a/src/org/ibex/util/ImmortalThread.java b/src/org/ibex/util/ImmortalThread.java new file mode 100644 index 0000000..90c5125 --- /dev/null +++ b/src/org/ibex/util/ImmortalThread.java @@ -0,0 +1,31 @@ +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the Apache Public Source License 2.0 ("the License"). +// You may not use this file except in compliance with the License. + +package org.ibex.util; +import java.util.*; +import java.net.*; +import java.io.*; + +/** run cycle() repeatedly, no more often than minCycleTimeMillis; catch and log any exceptions. */ +public abstract class ImmortalThread extends Thread { + + private final int minCycleTimeMillis; + public ImmortalThread() { this(500); } + public ImmortalThread(int minCycleTimeMillis) { this.minCycleTimeMillis = minCycleTimeMillis; } + + public abstract void cycle() throws Exception; + + public final void run() { + while(true) { + long now = System.currentTimeMillis(); + try { + cycle(); + } catch (Exception e) { Log.warn(this, e); } + long now2 = System.currentTimeMillis(); + if (now2 - now < minCycleTimeMillis) + try { Thread.sleep(minCycleTimeMillis - (now2 - now)); } catch (Exception e) { } + } + } + +}