Created
April 28, 2013 17:05
-
-
Save mgodave/5477511 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package org.robotninjas.util; | |
| import com.google.common.util.concurrent.AbstractFuture; | |
| import com.google.common.util.concurrent.TimeLimiter; | |
| import java.util.concurrent.Callable; | |
| import java.util.concurrent.RunnableFuture; | |
| import java.util.concurrent.TimeUnit; | |
| public class TimeLimitedFutureTask<V> extends AbstractFuture<V> implements RunnableFuture<V> { | |
| final TimeLimiter limiter; | |
| final TimeUnit unit; | |
| final long duration; | |
| final Callable<V> op; | |
| public TimeLimitedFutureTask(TimeLimiter limiter, TimeUnit unit, long duration, Callable<V> op) { | |
| this.limiter = limiter; | |
| this.unit = unit; | |
| this.duration = duration; | |
| this.op = op; | |
| } | |
| @Override | |
| public void run() { | |
| try { | |
| final V result = limiter.callWithTimeout(op, duration, unit, true); | |
| set(result); | |
| } catch (Exception e) { | |
| setException(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment