Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java LinkedBlockingQueue只返回多个线程中的一个_Java_Multithreading_Blockingqueue - Fatal编程技术网

Java LinkedBlockingQueue只返回多个线程中的一个

Java LinkedBlockingQueue只返回多个线程中的一个,java,multithreading,blockingqueue,Java,Multithreading,Blockingqueue,我创建了一个类,它计算同一目录中给定文件中的单词数。由于文件非常大,我决定使用多线程实现多个文件的计数 当按如下规定运行DriverClass时,它会卡在线程1上。 我做错了什么?当我在queue.take()上迭代时,人们会期望解析器等待检索并继续。在将()放入队列时,线程1被卡住使我怀疑有错误 提前谢谢你 驾驶员等级: public class WordCountTest { public static void main(String[] args){ if (ar

我创建了一个类,它计算同一目录中给定文件中的单词数。由于文件非常大,我决定使用多线程实现多个文件的计数

当按如下规定运行DriverClass时,它会卡在线程1上。 我做错了什么?当我在queue.take()上迭代时,人们会期望解析器等待检索并继续。在将()放入队列时,线程1被卡住使我怀疑有错误

提前谢谢你

驾驶员等级:

public class WordCountTest {
    public static void main(String[] args){
        if (args.length<1){
            System.out.println("Please specify, atleast, one file");
        }
        BlockingQueue<Integer> threadQueue = new LinkedBlockingQueue<>();
        Runnable r;
        Thread t;

        for (int i = 0; i<args.length; i++){
            r = new WordCount(args[i], threadQueue);
            t = new Thread(r);
            t.start();

            int total = 0;
            for (int k = 0; k<args.length; k++){
                try {
                    total += threadQueue.take();
                } catch (InterruptedException e){
                }
            }
            System.out.println("Total wordcount: " + total);
        }
    }
}
公共类WordCountTest{
公共静态void main(字符串[]args){

如果(args.length您正在等待第一个线程启动后的所有结果。可能您打算在所有线程启动后等待结果


注意:如果您创建的线程数超过了您的CPU数,那么速度可能会慢一些。我建议改用固定线程池。

问题在于您使用的是嵌套循环,而您应该使用两个单独的循环:一个用于启动
字数,另一个用于收集结果,例如

public class WordCountTest {
    public static void main(String[] args){
        Queue<Integer> threadQueue = new ConcurrentLinkedQueue<>();
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

        CountDownLatch latch = new CountDownLatch(args.length);

        for (int i = 0; i<args.length; i++){
            CompletableFuture.runAsync(new WordCount(args[i], threadQueue), executor)
                .thenRunAsync(latch.countDown(), executor);
        }

        latch.await();

        int sum = 0;
        for(Integer i : threadQueue) {
            sum += i;
        }
    }
}
公共类WordCountTest{
公共静态void main(字符串[]args){
队列线程队列=新的ConcurrentLinkedQueue();
ExecutorService executor=Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
CountDownLatch闩锁=新的CountDownLatch(参数长度);

对于(int i=0;iThreadcount over CPU’s已经实现了。正如上面提到的答案,在我写这篇文章的时候,我找到了嵌套循环的错误,并通过使用两个独立的循环修复了它-一个用于启动线程,一个用于提取结果。谢谢你的回答!是的,我找到了相同的解决方案并实现了it、 在这个答案之前。无论如何谢谢你!-我已经把你的答案标记为最终答案。谢谢你的时间!
public class WordCountTest {
    public static void main(String[] args){
        Queue<Integer> threadQueue = new ConcurrentLinkedQueue<>();
        ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

        CountDownLatch latch = new CountDownLatch(args.length);

        for (int i = 0; i<args.length; i++){
            CompletableFuture.runAsync(new WordCount(args[i], threadQueue), executor)
                .thenRunAsync(latch.countDown(), executor);
        }

        latch.await();

        int sum = 0;
        for(Integer i : threadQueue) {
            sum += i;
        }
    }
}