Package com.jcabi.aspects
Annotation Type Timeable
-
@Documented @Retention(RUNTIME) @Target(METHOD) public @interface Timeable
Makes a method time constrained.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 callThread.interrupt()
and hope that the thread itself is checking itsThread.isInterrupted()
status. If you want to design your long running methods in a way thatTimeable
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 } }
-
-
Field Summary
Fields Modifier and Type Fields Description static int
DEFAULT_LIMIT
The default maximum amount (of seconds).
-
-
-
-
unit
TimeUnit unit
Time unit for the limit.The minimum unit you can use is a second. We simply can't monitor with a frequency higher than a second.
- Returns:
- The time unit
- Default:
- java.util.concurrent.TimeUnit.SECONDS
-
-