java - Best approach to implement an accurate recorded stream execution -


i have stream of events recorded (for example arraylist<inputevent> sorted inputevent.getwhen()). differences in time between consecutive events can of order of tens of milliseconds.

my goal execute ("replay") recorded stream accurate possible, is, execute first event inputevent firstevent @ long starttime = system.currenttimemillis(), second @ starttime + (secondevent.getwhen() - firstevent.getwhen()), , on.

of course, accurate way be:

execute(arraylist<inputevent> stream) {     long starttime = system.currenttimemillis();     long firsttime = stream.get(0).getwhen();     (inputevent e : stream) {         while system.currenttimemillis() <                  starttime + e.getwhen() - firsttime) {  }         executeevent(e);     } } 

which terribly cpu consuming approach, on other hand:

execute(arraylist<inputevent> stream) {     long lastwhen = strem.get(0).getwhen();     (inputevent e : stream) {         try {             thread.sleep(e.getwhen() - lastwhen);         } catch (interruptedexception ex) {             ex.printstacktrace();         }         executeevent(e);         lastwhen = e.getwhen();     } } 

is better cpu, "subject precision , accuracy of system timers , schedulers" (http://docs.oracle.com/javase/6/docs/api/java/lang/thread.html#sleep(long)).

i considered timertaskexecutor think uses thread.sleep , suffers same subjectivity issue.

the question - should concerned of system dependent accuracy causing unexpected flow of stream? , there way trade accuracy cpu , somewhere in between 2 examples gave?


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -