背景:当需要进行大规模数据计算的时候,整体处理的效率较低,因此需要将数据分块计算然后,最终得到结果。
1、首先创建一个计算划分类
package com.smart.generic; import lombok.Getter; import lombok.Setter; import lombok.ToString; /** * * @author smart 2019/4/6 */ @Getter @Setter @ToString public class Calcualtor { private int indexBegin; private int indexEnd; private int []arr; private Long sum; }
2、实现Runnable接口
package com.smart.generic; import lombok.Getter; import java.util.concurrent.Callable; /** * * @author smart 2019/4/6 */ public class Calculate implements Callable <Long>{ @Getter private Calcualtor calcualtor; public Calculate (Calcualtor calcualtor){ this.calcualtor = calcualtor; } @Override public Long call() throws Exception { for(int i = calcualtor.getIndexBegin(); i<calcualtor.getIndexEnd(); i++) { calcualtor.setSum(calcualtor.