java - Cancelling a timer -


i have problem timer in logger class. created own logger should simple possible - user can call static log() method. every call log() method causes log object (message, time, additional information) adding list of logs.
when user calls method first time, logger starts new timer saves logs list file every 30seconds.

however, don't have idea call timer.cancel() method. don't want force user in code. necessary cancel timer? or method stop when user wants it.

but not need shutdown hook @ in case. when vm closed, threads cancelled anyway, , open files closed, no worries.

i believe want avoid task waking continuously every 30 s if app not doing anything. propose bit differently:

whenever log in log method:

  • store log entry
  • check if have timer task waiting executed (one boolean volatile 'taskrunning' flag). if not - fire task 30 seconds delay, once (not repetitive) , set taskrunning flag true. if flag set true - nothing.
  • in timer task: clear 'taskrunning' flag, write logs stored far , ... not fire task. no need. whenever writes log file, start task write logs 30 secs after.

note. in order avoid races need synchronisation on log read/write well.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -