如何在Java中创建线程限制

Afs*_*ami 13 java multithreading limit

假设我有1000个要读取的文件,并且由于某些限制,我想要并行读取最多5个文件.并且,一旦其中一个完成,我想要一个新的开始.

我有一个主函数谁拥有文件列表,我尝试在一个线程完成时更改计数器.但它不起作用!

有什么建议吗?

以下是主函数循环

for (final File filename : folder.listFiles()) {

    Object lock1 = new Object();
    new myThread(filename, lock1).start();
    counter++;
    while (counter > 5);
}
Run Code Online (Sandbox Code Playgroud)

Kyl*_*lar 22

像这样的产卵线程不是要走的路.使用a ExecutorService并将池指定为5.将所有文件放在类似一个BlockingQueue或另一个线程安全的集合中,所有正在执行的文件都可以随意使用poll().

public class ThreadReader {

    public static void main(String[] args) {
        File f = null;//folder
        final BlockingQueue<File> queue = new ArrayBlockingQueue<File>(1000);
        for(File kid : f.listFiles()){
            queue.add(kid);
        }

        ExecutorService pool = Executors.newFixedThreadPool(5);

        for(int i = 1; i <= 5; i++){
            Runnable r = new Runnable(){
                public void run() {
                    File workFile = null;
                    while((workFile = queue.poll()) != null){
                        //work on the file.
                    }
                }
            };
            pool.execute(r);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这有点矫枉过正.您不需要队列和ExecutorService.因为ExecutorService是一个固定的线程池,你可以只向它提交所有任务,并且它们将一次运行五个,直到它们全部完成.不需要队列. (3认同)

Pet*_*rey 6

您可以将 ExecutorService 用作线程池和队列。

ExecutorService pool = Executors.newFixedThreadPool(5);
File f = new File(args[0]);

for (final File kid : f.listFiles()) {
    pool.execute(new Runnable() {
        @Override
        public void run() {
            process(kid);
        }
    });
}
pool.shutdown();
// wait for them to finish for up to one minute.
pool.awaitTermination(1, TimeUnit.MINUTES);
Run Code Online (Sandbox Code Playgroud)