0

I have a trouble getting the proper value in the end of the function. I cannot understand how to make the main thread to wait for executor to finish, so I can display the proper value.

Each task calculates the product of values at index i in the arrays. I set the product variable to be volatile and synchronized the fucntion calculating the product to make sure that only one thread gets access to it and modifies it. But I cannot arrange the threads to wait to for executor to finish and then show the answer.

 public class NewBetterDot {
static double[] vectorOne;
static double[] vectorTwo;
private static volatile double product;;

public NewBetterDot(double[] vectorOne, double[] vectorTwo, double product) {
    super();
    this.vectorOne = vectorOne;
    this.vectorTwo = vectorTwo;
    this.product = product;

}

public static synchronized double smallDot(int i) {

    double multi = vectorOne[i] * vectorTwo[i];
    return multi;
}

static class Task implements Runnable {
    int i;

    @Override
    public void run() {

        // System.out.println("Before operation: " + product);

        product = product + NewBetterDot.smallDot(i);

        System.out.println(Thread.currentThread().getName() + " Product is equal to: " + product);

    }

    public Task(int i) {
        super();
        this.i = i;
    }

}

static class PrintTask implements Runnable {
    @Override
    public void run() {
        System.out.println("Product equals to: " + product);
    }
}

public static void main(String[] args) throws NullPointerException {

    double sum = 0.0;
    NewBetterDot objectOne = new NewBetterDot(new double[] { 5.0, 4.0, 1.0 }, new double[] { 1.0, 2.0, 3.0 }, 0.0);

    int threshhold = vectorOne.length;

    ExecutorService executor = Executors.newFixedThreadPool(2);
    ;

    for (int i = 0; i < threshhold; i++) {

        NewBetterDot.Task task = new NewBetterDot.Task(i);

        executor.submit(task);

    }

    executor.shutdown();

    NewBetterDot.PrintTask pt = new NewBetterDot.PrintTask();
    pt.run();
}

}

Blockquote

Lova Chittumuri
  • 2,994
  • 1
  • 30
  • 33
krysznys
  • 79
  • 1
  • 6

2 Answers2

0

The documentation for shutdown() says:

This method does not wait for previously submitted tasks to complete execution. Use awaitTermination to do that.

This code waits for up to 1 minute for the computation to stop:

executor.shutdown();
executor.awaitTermination(1, TimeUnit.MINUTES);
Joni
  • 108,737
  • 14
  • 143
  • 193
0

ExecutorService waits for all threads finish with the following method

taskExecutor.shutdown();
try {
  taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
  ...
}

from How to wait for all threads to finish, using ExecutorService?

-------update------

Your codes are really messy, because you use static fields and methods for thread task, as we usually use non-static objects. Based on your code, I made some changes to make the program work properly. The key point is that updating product operation should be an atomic, and the code is as follows

public static synchronized void addSmallDot(int i) {
    product += vectorOne[i] * vectorTwo[i];
    System.out.println(Thread.currentThread().getName() + " Product is equal to: " + product);
}

static class Task implements Runnable {
    int i;

    @Override
    public void run() {
        // System.out.println("Before operation: " + product);
        NewBetterDot.addSmallDot(i);
    }

    public Task(int i) {
        super();
        this.i = i;
    }

}

public static void main(String[] args) throws NullPointerException, InterruptedException {
    //...        
    executor.shutdown();
    executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    NewBetterDot.PrintTask pt = new NewBetterDot.PrintTask();
    pt.run();
}
Ticks
  • 528
  • 1
  • 5
  • 11
  • Thanks for taking time to answer. I tried doing it this way, the thing is, at the end of the program it can show different result. In the case above it often prints 16 as the result, but when run once again the result is 11, or sometimes, 8. – krysznys Apr 11 '20 at 17:04
  • I just updated the answer and the code works fine in my environment – Ticks Apr 11 '20 at 18:20