页面可以先返回啊,IO任务另开线程做。但页面可以做一个进度条的东西不断扫任务线程的进度,以提高用户体验。
|
|
页面js,定时去请求一个action,防止sesson超时。
|
|
建议新开线程,异步处理靠谱.
|
|
这个方法不错,进度条有用什么技术做呢?ajax? |
|
IO任务另开线程?怎么做啊 |
|
Thread每来一个文件开一个线程后台做,线程里面放一个isfinshed flag,程序判断Thread是否执行完毕
|
|
|
|
线程池,很方便
|
|
可以举个例子吗? |
|
20分 |
可以用ExecutorService 这个类,下面是一个用线程池时候下计算文件夹大小的例子,可以参考下
package cq.price; import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; public class ConcurrentTotalFileSize { public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { long start = System.nanoTime(); System.out.println("Total Size: " + new ConcurrentTotalFileSize() .getTotalFileSizeOfFilesInDir(new File("F:\资料"))); System.out.println("Time: " + (System.nanoTime() - start) / 1.0e9); } public long getTotalFileSizeOfFilesInDir(final File file) throws InterruptedException, ExecutionException, TimeoutException { if (file.isFile()) { return file.length(); } ExecutorService service = Executors.newFixedThreadPool(4); long total; try { total = 0; List<File> directories = new ArrayList<File>(); directories.add(file); while (!directories.isEmpty()) { final List<Future<SubDirectoriesAndSize>> partialResults = new ArrayList<Future<SubDirectoriesAndSize>>(); for (final File direcotry : directories) { partialResults.add(service .submit(new Callable<SubDirectoriesAndSize>() { @Override public SubDirectoriesAndSize call() throws Exception { return getTotalAndSubDirectories(direcotry); } })); } directories.clear(); for (Future<SubDirectoriesAndSize> future : partialResults) { SubDirectoriesAndSize directoriesAndSize = future.get(100, TimeUnit.SECONDS); if (directoriesAndSize == null) { continue; } total += directoriesAndSize.size; directories.addAll(directoriesAndSize.subDirectories); } } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage(), e); } finally { service.shutdown(); } return total; } public SubDirectoriesAndSize getTotalAndSubDirectories(final File file) { long total = 0; List<File> subDirectories = new ArrayList<File>(); if (file.isDirectory()) { File[] children = file.listFiles(); if (children != null) { for (File child : children) { if (child.isFile()) { total += child.length(); } else { subDirectories.add(child); } } } } return new SubDirectoriesAndSize(total, subDirectories); } } /** * 记录当初文件夹的下文件的大小以及子文件夹 * * @author Administrator * */ class SubDirectoriesAndSize { final public long size; final public List<File> subDirectories; public SubDirectoriesAndSize(long size, List<File> subDirectories) { super(); this.size = size; this.subDirectories = subDirectories; }; } |
只能多线程处理了,前端一个线程判断处理进度,后台可以考虑多线程处理任务。
|
|
请注意你的初衷: 一楼的回答包括两个步骤,步骤一其实已经解决了你的超时问题,至于步骤二,那属于优化的范畴——这个看你的需求,有没有必要做这样的优化?如果有这需求,再参考下进度条的解决方案来搞定吧! |
|
进度条只是用户体验,“IO任务另开线程”这个怎么做才是关键。 |
|
1楼说的不错 这种情况肯定是要异步处理的
|