added ImmortalThread
authoradam <adam@megacz.com>
Sat, 4 Feb 2006 10:09:24 +0000 (10:09 +0000)
committeradam <adam@megacz.com>
Sat, 4 Feb 2006 10:09:24 +0000 (10:09 +0000)
darcs-hash:20060204100924-5007d-821fa64f67025c65cf544c60495a08f5e68da2dc.gz

src/org/ibex/util/ImmortalThread.java [new file with mode: 0644]
src/org/ibex/util/Vec.java

diff --git a/src/org/ibex/util/ImmortalThread.java b/src/org/ibex/util/ImmortalThread.java
new file mode 100644 (file)
index 0000000..90c5125
--- /dev/null
@@ -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) { }
+        }
+    }
+
+}
index 2c55c6e..cffe9f5 100644 (file)
@@ -53,6 +53,7 @@ public final class Vec implements Serializable, Cloneable {
         return -1;
     }
     
+    public void add(Object o) { addElement(o); }
     public void addElement(Object o) {
         if (size >= store.length - 1) grow();
         store[size++] = o;
@@ -62,6 +63,7 @@ public final class Vec implements Serializable, Cloneable {
         return lastElement();
     }
 
+    public Object get(int i) { return elementAt(i); }
     public Object elementAt(int i) {
         return store[i];
     }
@@ -101,6 +103,7 @@ public final class Vec implements Serializable, Cloneable {
             store[i] = in[i];
     }
 
+    public void remove(int i) { removeElementAt(i); }
     public void removeElementAt(int i) {
         if (i >= size || i < 0) throw new RuntimeException("tried to remove an element outside the vector's limits");
         for(int j=i; j<size - 1; j++)