@Documented @Retention(value=RUNTIME) @Target(value=METHOD) public @interface Timeable
For example, this load() method should not take more than
 a second, and should be interrupted if it takes more:
 
 @Timeable(limit = 1, unit = TimeUnit.SECONDS)
 String load(String resource) {
   // something that runs potentially long
 }
 Important to note that in Java 1.5+ it is impossible to force thread
 termination, for many reasons. Thus, we can't
 just call Thread.stop(),
 when a thread is over a specified time limit. The best thing we can do is to
 call Thread.interrupt() and hope that the thread itself
 is checking its
 Thread.isInterrupted() status. If you want to design your long
 running methods in a way that Timeable can terminate them, embed
 a checker into your most intessively used place, for example:
 
 @Timeable(limit = 1, unit = TimeUnit.SECONDS)
 String load(String resource) {
   while (true) {
     if (Thread.currentThread.isInterrupted()) {
       throw new IllegalStateException("time out");
     }
     // execution as usual
   }
 }public abstract int limit
public abstract TimeUnit unit
The minimum unit you can use is a second. We simply can't monitor with a frequency higher than a second.
Copyright © 2012–2014 jcabi.com. All rights reserved.