项目中使用多线程方法-有返回参数

...
//设置量表作答数据 及 已保存报告数据
		ctx.setScaleRecordDtoFuture(ThreadUtil.submitNullable(ctx.getScaleId(), this::getScaleRecordData));
		//设置模板配置
		ctx.setTemplateConfigDtoFuture(ThreadUtil.submitNullable(ctx.getScaleTemplateId(), this::getTemplateConfig));
		
...
public Dto getScaleRecordData(){
	...业务逻辑处理
}
public Dto1 getTemplateConfig(){
	...业务逻辑处理
}

ThreadUtil

import com.X.X.X.exception.FutureGetException;

import java.util.Optional;
import java.util.concurrent.*;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * 线程池工具
 * @author super
 *
 */
public class ThreadUtil {
	
	private static final int DEFAULT_CORE_SIZE = 15;
    private static final long DEFAULT_KEEP_ALIVE = 60L;
    private static final int DEFAULT_QUEUE_SIZE = 1000;
    
    private static int FUTURE_DEFAULT_TIME_OUT_MS = 1500;
    
    /**
     * 默认通用线程池
     */
    public static ThreadPoolExecutor DEFAULT_POOL = new ThreadPoolExecutor(DEFAULT_CORE_SIZE,
            DEFAULT_CORE_SIZE, DEFAULT_KEEP_ALIVE, TimeUnit.SECONDS, new LinkedBlockingQueue<>(DEFAULT_QUEUE_SIZE),
            Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy());

    public static <V> Future<V> submit(Callable<V> callable) {
        return DEFAULT_POOL.submit(callable);
    }
    
    /**
     * value参数非空 执行 fun 否则返回null
     * @param 
     * @param 
     * @param value
     * @param fun
     * @return
     */
    public static <V, U> Future<U> submitNullable(V value, Function<V,U> fun) {
    	return Optional.ofNullable(value).map(e -> submit(() -> fun.apply(e))).orElse(null);
    }
    
    public static void execute(Runnable task) {
    	DEFAULT_POOL.execute(task);
    }

    public static <V> V getResult(Future<V> f) {
        try {
            return f.get(FUTURE_DEFAULT_TIME_OUT_MS, TimeUnit.MILLISECONDS);
        } catch (Exception e) {
            throw new FutureGetException(e);
        }
    }

    /**
     * f为空返回null
     * @param 
     * @param f
     * @return
     */
    public static <V> V getResultNullable(Future<V> f) {
    	return Optional.ofNullable(f).map(e -> getResult(e)).orElse(null);
    }
    
    /**
     * f为空返回null 转换fun 
     * @param 
     * @param 
     * @param f
     * @param fun 类型V转为类型U
     * @return
     */
    public static <V, U> U getResultNullable(Future<V> f, Function<V,U> fun) {
    	return Optional.ofNullable(f).map(e -> getResult(e)).map(fun).orElse(null);
    }
    
    /**
     * f为空返回指定默认值
     * @param 
     * @param f
     * @param d 指定的默认值
     * @return
     */
    public static <V> V getResultNullable(Future<V> f ,Supplier<V> d) {
    	return Optional.ofNullable(f).map(e -> getResult(e)).orElse(d.get());
    }

    
}

FutureGetException


/**
 * 异步获取异常
 * @author super
 *
 */
public class FutureGetException extends RuntimeException{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7674313720421106809L;
	
	private final Exception target;

	public FutureGetException(Exception e) {
		this.target = e;
	}
	
	public Exception getTarget() {
		return target;
	}

}

对象


	private Future<TemplateConfigDto> templateConfigDtoFuture;
	
	private Future<ScaleRecordDto> scaleRecordDtoFuture;
	

取出对象

public ScaleRecordDto getScaleRecordDto() {
		if(Objects.isNull(templateConfigDto)) {
			scaleRecordDto = ThreadUtil.getResultNullable(scaleRecordDtoFuture);
		}
		return scaleRecordDto;
	}
	

注释:其他future< Object>也是一样取出的。

你可能感兴趣的:(java,spring,intellij-idea,spring,boot,spring,cloud)