线程池实现

public class AsyncService {
    private ScheduledThreadPoolExecutor shceduleThreadPool = new ScheduledThreadPoolExecutor(5);
    private ThreadPoolExecutor threadPool = new ThreadPoolExecutor(10, 20, 600L, TimeUnit.SECONDS, new LinkedBlockingQueue());

    public void execute(final Runnable runnable){
        Runnable run = new Runnable(){
            @Override
            public void run() {
                try{
                    runnable.run();
                } catch(Exception e){
                    throw new RuntimeException(e);
                }
            }
        };
        threadPool.execute(run);
    }

}

使用

asyncService.execute(new Runnable() {
    @Override
    public void run() {
        TmsOrderInfoBO tmsOrderInfoBO = orderToTmsOrderInfoBO(order, createTime);
        tmsOrderInfoService.addTmsOrderInfo(tmsOrderInfoBO);
        tmsOrderInfoService.updateOrderResult(NumberCodeUtil.decode(order.getOrderId()), ResultCode.RESULT_NULL, null, new Date());
    }
});



你可能感兴趣的:(多线程)