我有一个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
并处理。
因此,我正在研究GPars,Java Thread and Queue,很快就会知道哪种方式有效,现有的解决方案是什么
答案 0 :(得分:2)
您正在寻找一个线程池,或者用Java术语来表示Executor
:
Executor executor = anExecutor();
executor.execute(aRunnable());
方法anExecutor
应该返回一个新的Executor
实例:
Executor anExecutor() {
return Executors.newFixedThreadPool(42); // just an example ...
}