Java有效实现Thread

时间:2014-10-27 11:58:53

标签: java multithreading groovy

我有一个file upload功能,用户可以同时上传多个文件,为了获得良好的效果,我使用了Thread processing这样的

Thread.start{
  // do file processing 'cause it is a long running process
}

现在的问题是,对于每个文件上传system都会创建一个new Thread,这会导致系统崩溃和其他一些问题,所以现在我正在寻找一个可以创建{的解决方案{1}}存储所有收到的文件,并一次创建Queue (say 5 nos)Thread并处理它,然后再创建一组Thread并处理。

因此,我正在研究GParsJava Thread and Queue,很快就会知道哪种方式有效,现有的解决方案是什么

1 个答案:

答案 0 :(得分:2)

您正在寻找一个线程池,或者用Java术语来表示Executor

Executor executor = anExecutor();
executor.execute(aRunnable());

方法anExecutor应该返回一个新的Executor实例:

Executor anExecutor() {
    return Executors.newFixedThreadPool(42); // just an example ...
}