消费者 class 未写入列表 多线程应用程序 Java

consumer class not writing to list Multi threaded application Java

我对这个消费者有疑问class。 当我 运行 程序时,池中的每个线程都出现越界异常。 我有 sysout 列表大小,由于某种原因它为零。 请在下面查看我的代码。 我对设置的列表没有任何兴趣,但我无法弄清楚。 该程序的每个其他部分都在工作和测试,只是最后一部分给我带来了问题。 如果能提供任何帮助,我们将不胜感激。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Consumer implements Runnable {
    private BlockingQueue<Shingle> q;
    private int k;
    private int[] minHashes;
    private Map<Integer, List<Integer>> map = new HashMap<>();
    private ExecutorService pool;

    public Consumer(BlockingQueue<Shingle> q, int k, int poolSize) {
        super();
        this.q = q;
        this.k = k;
        pool = Executors.newFixedThreadPool(poolSize);
        init();
    }

    public void init() {
        Random random = new Random();
        minHashes = new int[k];
        for (int i = 0; i < minHashes.length; i++) {
            minHashes[i] = random.nextInt();
        }
    }

    public void run(){
        try {
            int docCount = 2;
            while (docCount > 0) {
                Shingle s = q.take();
                if (s instanceof Poision) {
                    docCount--;
                } else {
                    pool.execute( new Runnable() {
                        public void run() {
                            for (int i = 0; i < minHashes.length; i++) {
                                int value = s.getHashCode() ^ minHashes[i]; // ^ - xor(Random generated key)
                                List<Integer> list = map.get(s.getDocId());
                                if (list == null) {
                                    list = new ArrayList<Integer>(k);
                                    for (int j = 0; j < list.size(); j++) {
                                        list.set(j , Integer.MAX_VALUE);
                                    }
                                    map.put(s.getDocId(), list);
                                } else {
                                    if (list.get(i) > value) {
                                        list.set(i, value);
                                    }
                                }
                            }
                        }
                    });

                }
            }
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

}

问题是这一行

list = new ArrayList<Integer>(k);

创建列表,初始容量 等于 k,而不是 大小 等于 k。所以你的列表有 0 个元素和 for 循环

for (int j = 0; j < list.size(); j++) {
   list.set(j , Integer.MAX_VALUE);
}

不执行任何迭代。然后,您将空列表添加到地图,因此 list.get(i) 抛出异常。

请将您的 for 循环更改为

for (int j = 0; j < minHashes.length; j++) {
    list.add(Integer.MAX_VALUE);
}

实际向列表中添加元素。