1

我在下面的代码中遇到了线程问题。当线程执行可运行对象的 Run 方法时,它不会打印我期望的数据。

代码1——调用代码

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

在上面的代码中,我检查了 CPEMethodData.getLogDataMap() 返回的 logData 与我预期的不同。但是当可运行对象实际执行时,它使用相同的数据运行......

代码2——创建5个线程的线程池...

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

代码3--可运行代码

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

下面是 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;
}

}

当我运行将两个可运行文件提交到队列的代码 1 时,我希望在 run 方法的 sysout 中看到不同的 logData 内容,但是当我调试它时,我发现两次执行中的数据都是相同的……似乎是第二个runnable 干扰了第一个..任何人都可以帮助我了解这里的问题是什么。我认为我正在传递 2 个不同的 CatalogLogger 实例并且不应该引起任何问题..也可以请任何人为此提出任何解决方案?

4

1 回答 1

0

正如@ReneLink 在对我的问题的评论中所写,CPEMethodData.getLogDataMap 正在返回哈希图的相同实例......所以当线程的运行方法被执行时,哈希图的内容正在被修改。我使用创建了哈希图的深层副本克隆工具并将其传递给线程。感谢@ReneLink 向我指出这一点。

于 2013-06-28T07:22:47.410 回答