-1

Hi I am doing a concurrent programming task in java where I'm using an executorService consisting of 10 threads. I have an array containing 100 elements all set initially to 1000. What I am trying to do is I have 2 indexes from the array, and I need to transfer a random amount from the first index to the second index, my problem is I'm not sure how to use the set function here in this case, as the second parameter of set should be a Future.

        var ex = Executors.newFixedThreadPool(10);

        List<Future<Integer>> elements = new ArrayList<>();

        for (int i = 0; i < 100; i++) {
            elements.add(ex.submit(() -> {
                int val = 1000;
                return val;
            }));
        }

        ex.shutdown();

        int sum = 0;
        for (Future<Integer> el : elements) {
            sum += el.get();
        }
        System.out.println("Initial sum: " + sum);

for (int i = 0; i < 10_000; i++) {
            ex.submit(() -> {
                int firstIndex = ThreadLocalRandom.current().nextInt(100);
                int secondIndex = ThreadLocalRandom.current().nextInt(100);
                int randomAmount = ThreadLocalRandom.current().nextInt(1000);
                try {
                    if (elements.get(firstIndex).get() - randomAmount > 0) {
                        elements.set(firstIndex,elements.get(firstIndex).get() - randomAmount);
                    }
                } catch (InterruptedException | ExecutionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            });
        }
sufocator
  • 39
  • 3

1 Answers1

0

The issue is that the elements array contains Futures. You're trying to put an integer into an array that contains Future objects, not integers. Futures are also immutable so you can't overwrite the value inside the Future either. Why are you creating the elements array by submitting tasks to the Executor? If you just create an array of integers you will fix your issue, but I'm not entirely clear what the point is of the code you're writing. Also you are shutting down the executor after submitting the first batch, so the next batch will not be able to be submitted. You will also have some concurrency issues since you'll have multiple threads reading and writing to the same array.

var ex = Executors.newFixedThreadPool(10);

List<Integer> elements = new ArrayList<>();

for (int i = 0; i < 100; i++) {
  elements.add(1000);
}

//ex.shutdown();

int sum = 0;
for (int el : elements) {
  sum += el;
}
System.out.println("Initial sum: " + sum);

for (int i = 0; i < 10_000; i++) {
  ex.submit(() -> {
      int firstIndex = ThreadLocalRandom.current().nextInt(100);
      int secondIndex = ThreadLocalRandom.current().nextInt(100);
      int randomAmount = ThreadLocalRandom.current().nextInt(1000);
      if (elements.get(firstIndex) - randomAmount > 0) {
          elements.set(firstIndex, elements.get(firstIndex) - randomAmount);
      }
  });
}