0

I have a problem with run the program. At one execution the list(HashMap) has a size and at another execution the HashMap product has different size. Below is the code of method in which I created the pool of thread...

public HashMap<Integer, MatrixIndex> getMultiplyMatrix(HashMap<Integer, MatrixIndex> rowsMap, HashMap<Integer, MatrixIndex> colsMap,
        int noThreads) throws InterruptedException {
    HashMap<Integer, MatrixIndex> product = new HashMap<Integer, MatrixIndex>();
    ExecutorService pool = Executors.newFixedThreadPool(noThreads);
    for (int i = 0; i < rowsMap.size(); i++) {
        pool.submit(new MultiplyMatrix(rowsMap, colsMap, product, i));
    }

    pool.shutdown();
    pool.awaitTermination(1, TimeUnit.DAYS);

    return product;
}

The method run() is

public void run() {

        Map<Integer, Double> mapRow = rowsMap.get(row).getMap();
        MatrixIndex s = new MatrixIndex();
        for (int j = 0; j < columnsMap.size(); j++) {
            Map<Integer, Double> mapCol = columnsMap.get(j).getMap();
            double sum = 0.0;
            for (Map.Entry<Integer, Double> entry : mapCol.entrySet()) {
                int val = entry.getKey();
                if (mapRow.containsKey(val)) {
                    sum += mapRow.get(val).doubleValue() * entry.getValue();
                }
            }

            s.add(j, sum);

        }
        product.put(row, s);
    }
}

And i call product = p.getMultiplyMatrix(rowsMap,columnsMap, 4); product.size() is different if I run several times the program. Is a problem with synchronize threads?? Thanks

dany
  • 11
  • 5

0 Answers0