Threadpool执行程序不更新并发hashmap

时间:2016-03-03 09:46:09

标签: java multithreading threadpoolexecutor concurrenthashmap

我使用线程池执行器生成5个线程并行执行5个不同的命令。在每个线程完成后,我将更新并发hashmap,其中threadid作为键的条目并终止为值。但是我的线程池没有更新成功完成命令执行的hashmap。

主类:

package com.cisco.executor;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class MainExecutor {

    static String element;
    static ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<Integer, String>();
    static Integer array[] = { 1, 2, 3, 4, 5 };
//  static Integer array[] = { 1 };
    static List<Integer> threadid = Arrays.asList(array);
    static String SQOOP_XXCCS_DS_SAHDR_CORE = ReadProperties.getInstance().getProperty("SQOOP_XXCCS_DS_SAHDR_CORE");
    static String SQOOP_XXCCS_DS_CVDPRDLINE_DETAIL = ReadProperties.getInstance()
            .getProperty("SQOOP_XXCCS_DS_CVDPRDLINE_DETAIL");
    static String SQOOP_XXCCS_DS_INSTANCE_DETAIL = ReadProperties.getInstance()
            .getProperty("SQOOP_XXCCS_DS_INSTANCE_DETAIL");
    static String SQOOP_XXCCS_SCDC_PRODUCT_PROFILE = ReadProperties.getInstance()
            .getProperty("SQOOP_XXCCS_SCDC_PRODUCT_PROFILE");
    static String SQOOP_MTL_SYSTEM_ITEMS_B = ReadProperties.getInstance().getProperty("SQOOP_MTL_SYSTEM_ITEMS_B");

    public static void main(String[] args) {

        ThreadPoolExecutor executors = (ThreadPoolExecutor) Executors.newFixedThreadPool(5);
//      ThreadPoolExecutor executors = (ThreadPoolExecutor) Executors.newFixedThreadPool(1);

        System.out.println("at executors step");
        List<String> getlist = getList();
        Iterator<Integer> itr2 = threadid.iterator();

        for (Iterator<String> itr = getlist.iterator(); itr.hasNext() && itr2.hasNext();) {
            String element = (String) itr.next();
            int thread_id = itr2.next();
            String[] command = { "ssh", "hddev-c01-edge-02", "\"" + element + "\"" };
            System.out.println("the command is as below ");
            System.out.println(Arrays.toString(command));
            System.out.println("inside the iterator");
            ParallelExecutor pe = new ParallelExecutor(command, thread_id, map);
            executors.execute(pe);
        }
        // executors.shutdown();
        for(Map.Entry<Integer, String> entry: map.entrySet())
        {
            Integer key = entry.getKey();
            String value = entry.getValue();            
            System.out.println("The key is " + key + " The value is " + value);
            System.out.println("Thread " + key + " is terminated");
        }

    }

    public static List<String> getList() {
        List<String> commandlist = new ArrayList<String>();
        System.out.println("inside getList");
        commandlist.add(SQOOP_XXCCS_DS_SAHDR_CORE);
        commandlist.add(SQOOP_XXCCS_DS_CVDPRDLINE_DETAIL);
        commandlist.add(SQOOP_XXCCS_DS_INSTANCE_DETAIL);
        commandlist.add(SQOOP_XXCCS_SCDC_PRODUCT_PROFILE);
        commandlist.add(SQOOP_MTL_SYSTEM_ITEMS_B);
        return commandlist;
    }

}

Runnable Class:

package com.cisco.executor;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

public class ParallelExecutor implements Runnable {

    private static Logger LOGGER = Logger.getLogger(ParallelExecutor.class);

    String[] command;
    int threadid;
    ConcurrentHashMap<Integer, String> map;

    public ParallelExecutor(String[] command, int threadid, ConcurrentHashMap<Integer, String> map) {
        this.command = command;
        this.threadid = threadid;
        this.map = map;
    }

    @Override
    public void run() {
        ProcessBuilder processbuilder = new ProcessBuilder(command);
        LOGGER.info(command);
        try {
            Process process = processbuilder.inheritIO().start();
            System.out.println("inside process builder ");
            process.waitFor();
            BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String readline;
            while ((readline = reader.readLine()) != null) {
                LOGGER.info(readline);
            }
            // getting the thread state and adding it to a collection
            Thread.State state = Thread.currentThread().getState();
            if (state == Thread.State.TERMINATED) {
                map.put(threadid, "TERMINATED");
            }
        } catch (Exception e) {
            LOGGER.error(e.getMessage());
        }
    }

}

我的实施是否错误。有人可以帮我实现。

2 个答案:

答案 0 :(得分:1)

我没有尝试捕获线程中线程的结果(如果抛出异常/错误,则容易出错),我建议您保留Future对象并检查它们。

    ExecutorService exec = Executors.newFixedThreadPool(5);

    System.out.println("at executors step");
    Map<String, Future<?>> results = new HashMap<>();
    for (String element : getList()) {
        String[] command = { "ssh", "hddev-c01-edge-02", "\"" + element + "\"" };
        results.put(element, exec.submit(new ParallelExecutor(command, thread_id, map)));
    }
    for(Map.Entry<String, Future<?>> entry: map.entrySet()) {
        try {
            entry.getValue().get();
            System.out.println(entry.getKey()+ " is complete");
        } catch (ExecutionException e) {
            System.out.println(entry.getKey()+ " failed with");
            e.getCause().printStackTrace(System.out);
        }
    }

答案 1 :(得分:0)

ThreadPoolExecutor不会终止,直到它被要求这样做。 所以,首先你要打电话

// executors.shutdown();

,你保持评论。 2,你需要等待线程正确终止。为此添加循环,之前为(Map.Entry条目:map.entrySet())

while (!es.isTerminated()) {
        }

但是,因为一个线程可能会运行许多可运行的东西,如果我正确地找到你,你想在一个Runnable执行完毕后更新CHM。

为此,您必须使用CustomThread类。扩展线程 并覆盖只有1个方法afterExecute(),您需要在其中放置代码以使用Runnable的id和终止状态更新CHM。但请记住,这意味着完成传递的Runnables run()方法,而不是底层Thread的终止。