public class OrderReadThread implements Runnable { private List<Map<String, Object>> orderList; private BaseDalClient dalClient; private Map<String, Object> params; private int totalRecords; private CyclicBarrier cyclicBarrier; @SuppressWarnings({ "rawtypes", "unchecked" }) @Override public void run() { // 每个线程分页查询,最终将结果放入公共的orderList Page<?> pageInfo = dalClient.queryForListPage("eps.UserRegisterServiceImpl.selectOrderList", params, totalRecords); List tempList = pageInfo.getList(); orderList.addAll(tempList); try { // 子线程执行结束,等待其他未执行完的线程,如果所有线程都执行结束即执行同步线程 cyclicBarrier.await(); } catch (InterruptedException e) { e.printStackTrace(); } catch (BrokenBarrierException e) { e.printStackTrace(); } } public void setOrderList(List<Map<String, Object>> orderList) { this.orderList = orderList; } public void setDalClient(BaseDalClient dalClient) { this.dalClient = dalClient; } public void setParams(Map<String, Object> params) { this.params = params; } public void setTotalRecords(int totalRecords) { this.totalRecords = totalRecords; } public void setCyclicBarrier(CyclicBarrier cyclicBarrier) { this.cyclicBarrier = cyclicBarrier; } }
public class FileWriteTaskThread implements Runnable { private List<Map<String, Object>> orderList; public FileWriteTaskThread(List<Map<String, Object>> orderList) { this.orderList = orderList; } @Override public void run() { System.out.println("****************将所有子线程统计的最终的结果保存到文件中F*******************"); String outFileName = "order" + getCurrentTimeStr() + ".txt"; String outFilePath = "d:/order/" + outFileName; BufferedWriter bw = null; try { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFilePath), "UTF-8")); for (Map<String, Object> order : orderList) { StringBuilder line = new StringBuilder(200); line.append(MapUtils.getString(order, "b2corderno", "")).append(",") .append(MapUtils.getString(order, "phonenum", "")).append(",") .append(MapUtils.getString(order, "customername", "")).append(",") .append(MapUtils.getString(order, "customeraddress", "")).append(",") .append(MapUtils.getString(order, "suppliercode", "")); bw.write(line.toString()); bw.newLine(); } } catch (Exception e) { e.printStackTrace(); } finally { if (bw != null) { IOUtils.closeQuietly(bw); } } } private String getCurrentTimeStr() { Date d = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss"); return format.format(d); } }
/** * 每次查询1000条 */ private static final int PERCENT_DOWNLOAD_NUM = 1000; public static void main(String[] args) { // 启动Spring容器 ApplicationContext context = new FileSystemXmlApplicationContext( "file:src/main/webapp/WEB-INF/applicationContext.xml"); // 获取数据库操作类 BaseDalClient dalClient = context.getBean("dalClient", BaseDalClient.class); // 查询总记录数 int totalRecords = dalClient .queryForObject("eps.UserRegisterServiceImpl.selectOrderCount", null, Integer.class); int temp = totalRecords / PERCENT_DOWNLOAD_NUM; int queryCount = (totalRecords % PERCENT_DOWNLOAD_NUM) != 0 ? temp + 1 : temp; List<Map<String, Object>> orderList = Collections.synchronizedList(new ArrayList<Map<String, Object>>());// 线程安全的List // 同步辅助类,当所有子线程全部执行结束后来执行不同辅助类指定的同步线程FileWriteTaskThread CyclicBarrier cyclicBarrier = new CyclicBarrier(queryCount, new FileWriteTaskThread(orderList)); for (int i = 1; i <= queryCount; i++) { Map<String, Object> params = new HashMap<String, Object>(); params.put("pageSize", PERCENT_DOWNLOAD_NUM); params.put("page", i); OrderReadThread orderReadThread = new OrderReadThread(); orderReadThread.setDalClient(dalClient); orderReadThread.setOrderList(orderList); orderReadThread.setParams(params); orderReadThread.setTotalRecords(totalRecords); orderReadThread.setCyclicBarrier(cyclicBarrier); // 启动多线程来从数据库中分页读取数据 Thread thread = new Thread(orderReadThread); thread.start(); } }