java多线程

一、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> {
	
	//保存查询参数的Map
	private Map<String, Object> paramMap;
	//业务Service  使用组合模式
	private MyService myService;
	//分页Service
	private 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;
	}



	//需要多线程处理的业务逻辑
	@Override
	public 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;

@Controller
public class MyController {
	
	@Resource
	private MyService myService;
	@Resource
	private 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();
					}
				}
			}
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值