1.实例框架服务
ExecutorService service=new
ThreadPoolExecutor(3, 5, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(10000)); 队列有阻塞、非阻塞、有界或无界之分,根据不同场景择优。
2.任务
public class MyCallable implements Callable<RetrunType> {
private static final Logger log = LoggerFactory.getLogger(ValidateCall.class);
......
public MyCallable (...) {
......
}
public RetrunType call() throws Exception {
......
}
}
3.提交任务到服务
MyCallable call=new MyCallable();
Future f = service.submit(call)
;
4.得到返回结果
f.get()
;
线程在执行提交任务后会一直在此处等待,直到get返回。