简体   繁体   中英

Thread issue while using Executor service

I have facing thread issue in the below code.When then thread executes the Run method of the runnable object,it doesnt print the data that I expect it to be.

code 1--calling code

Map<String,Object> logData = CPEMethodData.getLogDataMap();
    CatalogUpdaterLogger.getLogger().info("6 before new splunk logger log data =" + logData);
    CatalogrLogger writer = new CatalogLogger(LogType.INFO,logData,LoggerType.CATALOGUPDATER);
    LogPool.INSTANCE.submitTask(writer);//submitting writer which is a runnable object to the queue

//add one more task/writer to the queue in the same method

logData = CPEMethodData.getLogDataMap();
     CatalogUpdaterLogger.getLogger().info("11 before 3rd writer=logData "+logData);
     CatalogLogger writer2 = new CatalogLogger(LogType.INFO,logData,LoggerType.CATALOGUPDATER);

     LogPool.INSTANCE.submitTask(writer2);

In the above code,I have checked that logData Returned by the CPEMethodData.getLogDataMap()is different which I expected.But still when the runnable object actually executes,it runs with same data...

code 2--creating thread pool with 5 threads...

public enum LogPool {

    INSTANCE;
    private static final int nThreads = 5;
    final ExecutorService executor = Executors.newFixedThreadPool(nThreads);

    public synchronized void submitTask(Runnable task) {        
        executor.execute(task);     
    }

Code 3--runnable code

public class CatalogLogger implements Runnable {
    protected LogType logType;
    protected LoggerType loggerType;
    protected Map<String, Object> logData;
    public CatalogLogger(LogType logType, Map<String, Object> logData,
            LoggerType loggerType) {
        this.logType = logType;
        this.logData = logData;
        this.loggerType = loggerType;
    }

public void run() {
        System.out.println("running with logData " + logData);
        System.out.println(" Thread.currentThread().hashCode()  " +Thread.currentThread().hashCode());
        switch (loggerType) {
        case ORDERPROCESSING:
            logData(Logger.getLogger(ORDER_LOG));
            break;
        case CATALOGUPDATER:
            logData(Logger.getLogger(CATALOGUPDATER_LOG));
            break;
        }
    }

Below is the CPEmethoddata.getLogData

public class CPEMethodData {
private static ThreadLocal<Map<String, Object>> logDataMap = new ThreadLocal<Map<String, Object>>();

    public static Map<String,Object> getLogDataMap() {
        return logDataMap.get();
    }
public static void setOppParameters(Map<String, Object> inputParams) {
    Map<String, Object> oppStatus = logDataMap.get();
    if (oppStatus == null) {
        oppStatus = new HashMap<String, Object>();
        logDataMap.set(oppStatus);
    }
    oppStatus.put(INPUT_PARAMS, inputParams);
}

@SuppressWarnings("unchecked")
public static Map<String, Object> getOperationParameters() {
    Map<String, Object> oppStatus = logDataMap.get();
    if (oppStatus != null)
        return (Map<String, Object>) oppStatus.get(INPUT_PARAMS);
    return null;
}

}

when I run the code 1 which submits two runnable to the queue,I expect to see different logData content in the sysout of the run method but as i have debugged it I see that data is same in both the executions...seems that 2nd runnable is interfering with the first one....Can anyone please help me to understand what is the problem here.I thought I am passing 2 different instances of CatalogLogger and shouldnt cause any problem..Also can anyone please suggest any solution for this ?

As written by the @ReneLink in the comment to my question ,CPEMethodData.getLogDataMap was returning same instance of the hashmap...So by the time thread's run method was getting executed hashmap's content were getting modified.I created deep copy of the hashmap using Cloner facility and passed the same to the thread. Thanks @ReneLink for pointing out this to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM