首页 \ 问答 \ 使用Executor服务时出现线程问题(Thread issue while using Executor service)

使用Executor服务时出现线程问题(Thread issue while using Executor service)

我在下面的代码中面临线程问题。当线程执行runnable对象的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与我期望的不同。但是当runnable对象实际执行时,它仍以相同的数据运行...

代码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向队列提交两个runnable时,我希望在run方法的sysout中看到不同的logData内容,但是当我调试它时,我看到两个执行中的数据相同...似乎是第二个runnable正在干扰第一个....有谁可以请帮助我理解这里的问题。我以为我通过2个不同的CatalogLogger实例,不应该导致任何问题..也可以有人请建议任何解决方案?


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 ?


原文:https://stackoverflow.com/questions/17205069
更新时间:2024-01-08 16:01

最满意答案

用这个:

// orderHeader [TASKNAME /文本()= 'TEST_LABEL1'] /的TaskID /文本()


Use this:

//orderHeader[taskName/text()='TEST_LABEL1']/taskId/text()

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。