java多线程

来源:互联网 发布:js获取客户端ip地址 编辑:程序博客网 时间:2024/06/18 07:46

一、description

     项目中有个需求是要将查询出的1000万+的数据做一些处理后,再保存到数据库中,业务很简单,为了提高效率采用了多线程处理。
       基本思路:
1、将查询出的数据分页
2、开启一个线程池循环处理每一页的数据

下面的代码写得很初略,大体表现一个多线程简单的运行实例。

二、code

处理每页数据的类   这是实现的是Callable
package callable;import java.util.List;import java.util.Map;import java.util.concurrent.Callable;/** * 需要被多线程调用的类 * @author leiwei * */public class MyRunnable implements Callable<Object> {//保存查询参数的Mapprivate Map<String, Object> paramMap;//业务Service  使用组合模式private MyService myService;//分页Serviceprivate PageService pageService;//当前页   及要查询的第几页的数据int currentPage;public MyRunnable(Map<String, Object> paramMap, MyService myServcie,PageService pageService, int currentPage) {super();this.paramMap = paramMap;this.myService = myServcie;this.pageService = pageService;this.currentPage = currentPage;}//需要多线程处理的业务逻辑@Overridepublic Object call() throws Exception {int perPageNumber = 9000;//每页的数据量//分页查询PageDTO pageDTO = pageService.queryForPaginationList("queryForList.sql", paramMap, currentPage, perPageNumber);if (pageDTO != null) {//获取当前页的数据List<Map<String, Object>> data = pageDTO.getData();if(data != null && data.size() != 0) {//遍历data  执行一些操作后插入另一张表for (Map<String, Object> map : data) {try {map.put("column1", myService.query("sql1", map));map.put("column2", myService.query("sql2", map));map.put("column3", myService.query("sql3", map));//执行插入myService.insert("insert.sql", map);} catch (Exception e) {// TODO }}}}return null;}}

开启多线程的方法
package callable;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Future;import javax.annotation.Resource;import org.springframework.stereotype.Controller;@Controllerpublic class MyController {@Resourceprivate MyService myService;@Resourceprivate PageService pageService;/** * 开启多线程的方法 * @param paramMap */public void executeRunnable(Map<String, Object> paramMap){//开启一个拥有10个线程的线程池ExecutorService executorService = Executors.newFixedThreadPool(10);List<Future> result = new ArrayList<Future>();//查询总数据量int totalCount = (int) myService.query("queryTotalCount.sql", paramMap);int perPageNumber = 9000;//设定每个线程执行的数目为9000//计算出循环的调用线程池的次数   int whileNum = (int) Math.ceil((double)totalCount/(double)perPageNumber);try {for (int i = 0; i < whileNum; i++) {//记录当前执行的页数int currentPage = i + 1;MyRunnable myRunnable = new MyRunnable(paramMap, myService,pageService, currentPage);//线程池调用Future future = (Future)executorService.submit(myRunnable);result.add(future);}} catch (Exception e) {// TODO: handle exception} finally {//关闭线程池executorService.shutdown();//根据线程返回值建立阻塞的死循环while(result.size() > 0){Iterator<Future> it = result.iterator();while(it.hasNext()){Future f = it.next();//判断线程是否执行完成,如果完成则从result中去除if (f.isDone()) {it.remove();}}}}}}



原创粉丝点击