Maybe something like this:
import java.util.PriorityQueue;
public interface MyRunnable
{
public void run(Object value);
}
public class MyTimer implements Comparable{
private static PriorityQueue heap
private static long myTime;
private MyRunnable runnable;
private Object value;
private long time;
private boolean loop;
private long loopDuration;
public static void init()
{
myTime=0;
heap = new PriorityQueue();
}
public static void update(float tpf)
{
if(heap.isEmpty()==false)
{
myTime += (long)(tpf*1000);
while(heap.isEmpty()==false && ((MyTimer)heap.peek()).time < myTime )
{
MyTimer t = (MyTimer)heap.peek();
heap.remove();
t.execute();
}
}
}
public MyTimer(MyRunnable runnable, Object value, int time, boolean loop )
{
this.runnable=runnable;
this.value=value;
this.time = myTime+(long)time;
this.loop=loop;
if(loop)
{
loopDuration = (long)time;
}
heap.add(this);
}
private void execute()
{
if(runnable != null)
{
runnable.run(value);
if(loop)
{
time = time + loopDuration;
heap.add(this);
}else{
value=null;
runnable=null;
}
}
}
public void cancel()
{
value=null;
runnable=null;
heap.remove(this);
}
public int compareTo(Object o)
{
return (int)(time -((MyTimer)o).time);
}
}
Example:
public void simpleInitApp() {
MyTimer.init();
new MyTimer(new PrintMessage(),"loop",1000,true);
new MyTimer(new PrintMessage(),"first",500,false);
new MyTimer(new PrintMessage(),"third",5000,false);
new MyTimer(new PrintMessage(),"second",2000,false);
}
public void simpleUpdate(float tpf){
MyTimer.update(tpf);
}
private class PrintMessage implements MyRunnable
{
public void run(Object o)
{
System.out.println("Test:"+o);
}
}