0

The Executor Service appears to end early and not run every iteration

I am trying to run every iteration of multiple lists which should give a total of 18,900,000 runs of a method. However upon testing and running with a int to record the times it runs it gets close to 18,900,000 but never to it

package main;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test
{
    public static int count = 0;
    private static final DecimalFormat decimalFormat = new DecimalFormat("#.###");
    
    public static void main(String[] args)
    {
        int aStart = 140;
        int aEnd = 400;
        int aIncrement = 20;
        
        double bStart = 9;
        double bEnd = 27;
        double bIncrement = 0.75;
        
        int cStart = 14;
        int cEnd = 18;
        int cIncrement = 1;
        
        int dStart = 90;
        int dEnd = 180;
        int dIncrement = 30;
        
        int eStart = 60;
        int eEnd = 120;
        int eIncrement = 30;
        
        int fStart = 130;
        int fEnd = 250;
        int fIncrement = 30;
        
        double gStart = 5.5;
        double gEnd = 6.7;
        double gIncrement = 0.3;
        
        double hStart = 2.8;
        double hEnd = 4.2;
        double hIncrement = 0.2;
        
        double iStart = 2.6;
        double iEnd = 4;
        double iIncrement = 0.2;
        
        List<Integer> listA = list(aStart, aEnd, aIncrement);
        List<Double>  listB = list(bStart, bEnd, bIncrement);
        List<Integer> listC = list(cStart, cEnd, cIncrement);
        List<Integer> listD = list(dStart, dEnd, dIncrement);
        List<Integer> listE = list(eStart, eEnd, eIncrement);
        List<Integer> listF = list(fStart, fEnd, fIncrement);
        List<Double>  listG = list(gStart, gEnd, gIncrement);
        List<Double>  listH = list(hStart, hEnd, hIncrement);
        List<Double>  listI = list(iStart, iEnd, iIncrement);
        
        final int MAX_THREADS = 2;
        ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS);
        
        List<String> row = new ArrayList<>();
        
        for (int a : listA)
        {
            for (double b : listB)
            {
                for (int c : listC)
                {
                    for (int d : listD)
                    {
                        for (int e : listE)
                        {
                            for (int f : listF)
                            {
                                for (double g : listG)
                                {
                                    for (double h : listH)
                                    {
                                        for (double i : listI)
                                        {
                                            if (i < h) 
                                            {
                                                executor.submit(() -> row.addAll(calculateRecords("C://CSV", a, b, c, d, e, f, g, h, i)));
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        
        executor.shutdown();
        try
        {
            while (!executor.isTerminated())
            {
                // Wait for all tasks to complete
            }
            System.out.println("TEST COMPLETE");
        } catch (Exception e)
        {
            System.out.println("Thread execution interrupted.");
            Thread.currentThread().interrupt();
        }

        System.out.println(count);
    }
    
    public static ArrayList<Integer> list(int startPoint, int endPoint, int incrementPoint)
    {
        ArrayList<Integer> a = new ArrayList<>();
        for (int queryPoint = startPoint; queryPoint <= endPoint; queryPoint += incrementPoint)
        {
            a.add(queryPoint);
        }
        
        return a;
    }
    
    public static ArrayList<Double> list(double startPoint, double endPoint, double incrementPoint)
    {
        ArrayList<Double> a = new ArrayList<>();
        double tolerance = incrementPoint / 2.0;
        for (double queryPoint = startPoint; queryPoint <= endPoint + tolerance; queryPoint += incrementPoint)
        {
            double formatPoint = Double.parseDouble(decimalFormat.format(queryPoint));
            a.add(formatPoint);
        }
        return a;
    }
    
    public static List<String> calculateRecords(String directory, int a, double b, int c, int d, int e, int f, double g, double h, double i)
    {
        List<String> listC = new ArrayList<>();
        count++;
        return listC;
    }
}

If I run the above with 1 thread then it returns count as the expected number. Any increase of thread counts just makes it not run the correct number of times. I need it to run every iteration every time and it needs to be multithreaded so I am currently at odds with what to do.

Thanks

0 Answers0