guava - How can I know my Stopwatch has run? -


i use several stopwatches in application. created together, of them have run (due exceptions earlier in code or other things).

after application has run, i'm creating report using stopwatches. instance, i'm doing following:

stopwatch substopwatch = stopwatch.createunstarted(); stopwatch mainstopwatch = stopwatch.createstarted(); try {   // 1   substopwatch.start();   // 2   substopwatch.stop(); } {   mainstopwatch.stop();   system.out.printf("total run time: %s%n",  mainstopwatch);   if (!substopwatch.isrunning()) {     system.out.printf("  including sub run time: %s%n", substopwatch);   } } 

the problem in code if happens in "do 1" (return, exception), substopwatch printed anyways.

the following solutions work: - using boolean indicate started stopwatch. - using stopwatch more locally , using report mechanism contains information i'm looking for.

but main question remains: can know stopwatch has run using stopwatch only.

you can check elapsed time on stopwatch:

if (substopwatch.elapsed(timeunit.nanoseconds) > 0) {     // ran } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -