'Execution Flow for Executor Service

I am using Executor Service for multithreading. I have the below code:

ExecutorService executor = Executors.newFixedThreadPool(100);
final Multimap<String, String> queueIds = ArrayListMultimap.create();

for (final String sId : sMultiMap.keySet()) {
    for (final String gL : gsList) {
        for (final String sL : ssList) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    System.out.println(sId);
                    JSONArray guList = null;
                    JSONArray coJsonArray;
                    JSONObject arrayGuidObject, coResults = null;
                    String skillSetParam;
                    String url;
                    int result_count;
                    Long queue_id;
                    skillSetParam = "parameters";
                    url = " here is the url";
                    try {
                        URL urlEncoded = new URL(url);
                        URI uri = new URI(urlEncoded.getProtocol(), urlEncoded.getUserInfo(),
                                urlEncoded.getHost(), urlEncoded.getPort(), urlEncoded.getPath(),
                                urlEncoded.getQuery(), urlEncoded.getRef());
                        urlEncoded = uri.toURL();

                        guList = // get a list from API

                        for (int i = 0; i < guList.length(); i++) {
                            arrayGuidObject = guList.optJSONObject(i);

                            if (arrayGuidObject != null) {
                                System.out.println("Here 1");
                            }
                        }
                        coJsonArray = coResults.getJSONArray("results");
                        if (coResults != null) {
                            result_count = coResults.getInt("resultcount");
                            if (result_count > 0) {
                                for (int k = 0; k <= coJsonArray.length() ; k++) {
                                    JSONObject j = coJsonArray.getJSONObject(k);
                                    queue_id = j.getLong("id");
                                    queueIds.put(sId + "-" + gL + "-" + sL,
                                            "\"" + Long.toString(queue_id) + "\"");
                                }
                            }
                        }
                        int size = queueIds.keySet().size();
                        int flag = 0;
                        BufferedWriter writer = new BufferedWriter(
                                new OutputStreamWriter(new FileOutputStream("folderPath"
                                        + sId + "-" + gL + "-"
                                        + sL.replace(" ", "").replaceAll("/", "_") + ".json"),
                                        "utf-8"));
                        try {
                            System.out.println("Entered here");
                            for (String sGS : queueIds.keySet()) {
                                if (flag == 0) {
                                    writer.write("{");
                                }
                                flag++;
                                if (flag == size) {
                                    writer.write("\"" + sGS + "\"" + ": "
                                            + queueIds.get(sGS).toString());
                                } else {
                                    writer.write("\"" + sGS + "\"" + ": "
                                            + queueIds.get(sGS).toString() + ",");
                                }

                                if (flag == size) {
                                    writer.write("}");
                                }
                            }
                            System.out.println("Done");
                        } finally {
                            writer.close();
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    queueIds.clear();
                }
            });

            System.out.println("queueIds " + queueIds);

        }
    }
}
executor.shutdown();
executor.awaitTermination(24L, TimeUnit.HOURS);
} catch (Exception e) {
    e.printStackTrace();
}

So here i am not able to find the correct execution flow. When i run the program, it prints the empty queueIds First, which is after the execute. So my intention here is to open 100 threads at a time which loops through 100 values of the loop and i want to see the line by line execution here. I want to see sID which i am printing in the first line after the execute. Can anyone please suggest the flow of execution.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source