执行者服务的执行流程

时间:2017-08-11 15:36:59

标签: java multithreading executorservice flow

我正在使用Executor Service进行多线程处理。我有以下代码:

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();
}

所以在这里我无法找到正确的执行流程。当我运行程序时,它会打印空queueIds First,这是在执行之后。所以我的意图是一次打开100个线程,循环遍历100个循环值,我想在这里看到逐行执行。我想在执行后看到我在第一行打印的sID。任何人都可以建议执行流程。

0 个答案:

没有答案