public interface Invokable < T >{public T invoke ();public T returnIfFail ();}import java.util.concurrent.* ;public class InvokeWrapper < T >{private final static ExecutorService executor = Executors . newCachedThreadPool ();private Invokable < T > invokable ;public InvokeWrapper ( Invokable < T > invokable ){this . invokable = invokable ;}public T invokeWithTimeout ( long milliSec ){T result = null ;Future < T > future = this . executor . submit ( new Callable < T >(){@Overridepublic T call () throws Exception{return InvokeWrapper . this . invokable . invoke ();}});try{result = future . get ( milliSec , TimeUnit . MILLISECONDS );}catch ( Exception e ){result = this . invokable . returnIfFail ();}return result ;}}
异步线程包装器
最新推荐文章于 2018-06-26 14:14:02 发布