public static void main( String[] args ) { long time1 = new Date().getTime(); /*for(int s=1;s<=3;s++){ HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(s+""); for (int i=0;i<50000;i++){ String str = "DemoTest测试"; str+= Math.random(); HSSFRow row = sheet.createRow(i); HSSFCell cell = row.createCell(0); cell.setCellValue(str); } sheet.autoSizeColumn(0); }*/ List<ForkTask> taskList = new ArrayList<>(); for(int s=1;s<=3;s++){ ForkTask forkTask = new ForkTask(s + ""); taskList.add(forkTask); } for(ForkTask task : invokeAll(taskList)){ task.join(); } long time2 = new Date().getTime(); System.out.println("用时:"+(time2-time1)); } private static class ForkTask extends RecursiveAction { private String sheetName; public ForkTask(String sheetName) { this.sheetName = sheetName; } @Override protected void compute() { HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet(sheetName); for (int i=0;i<50000;i++){ String str = "DemoTest测试"; str+= Math.random(); HSSFRow row = sheet.createRow(i); HSSFCell cell = row.createCell(0); cell.setCellValue(str); } sheet.autoSizeColumn(0); } }
sheet.autoSizeColumn(0); 是一个相当耗性能的操作,普通for循环大概18秒,并发循环9秒左右;