使用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
最满意答案
用这个:
// orderHeader [TASKNAME /文本()= 'TEST_LABEL1'] /的TaskID /文本()
Use this:
//orderHeader[taskName/text()='TEST_LABEL1']/taskId/text()
相关问答
更多-
Jmeter - 正则表达式提取器 - 多个随机变量(Jmeter - Regular expression extractor - multiple random variable)[2021-11-19]
您需要调用预定义的变量vars 例如,如果您有一个名为idList_7的变量,其中包含一些值,则可以使用以下Beanshell代码访问该值 String s = vars.get("idList_7"); 或者如果你想使用串联 int seven = 7; String s = vars.get("idLst_" + seven); 有关暴露给Beanshell的JMeter组件的更多详细说明,请参见如何使用Beanshell指南。 You need to call a pre-defined vari ... -
根据您的错误的WARN级别,您看起来已经选中了Show warnings或Report Errors或两者。 如果您的页面不符合XHTML / XML,则需要选中Use Tidy 。 如果您的服务器响应从Tidy的角度来看“过于破碎”,您可以始终考虑使用后处理器来获取所需的数据: 正则表达式提取器 - 不关心错误或无效的标记 CSS / JQuery Extractor - 使用不同的选择器,不要求页面符合XML / XHTML。 一般情况下,我建议使用HTML Assertion检查页面,因为描述的情况对 ...
-
st ... -
改为使用正则表达式提取器: .value =“(。+?)”name =“AUTH_STATE”。 比赛数:1 匹配[1] [0] = value =“09a02539-e89c-4118-bc52-185330ef36e4”name =“AUTH_STATE”> 匹配[1] [1] = 09a02539-e89c-4118-bc52-185330ef36e4 希望这会有所帮助。 Through XPATH extractor, if i use this i am getting the values.. ...
-
如何通过JMeter中的XPath Extractor提取响应?(How to to extract response by XPath Extractor in JMeter?)[2023-05-31]
我已经在采样器中添加了一个Xpath提取器: 参考名称:mytest XPath查询: //test[@id='1'] 但是返回变量(mytest)是错误的。 OUT.println(mytest) --> void 显然println()函数打印test元素的字符串值,并且在提供的XML文档中, test元素没有任何内容,并且它们的字符串值是空字符串。 你想要 : /*/*/test[@id=1]/@name 和 /*/*/test[@id=1]/@status 前者选择文档顶部元素的所有tes ... -
如何从jmeter响应中提取正则表达式提取器?(how to extract Regular expression extractor from jmeter response?)[2023-08-05]
使用以下正则表达式: ChemicalID=(\d+) 演示: 参考文献: JMeter:正则表达式 使用RegMe(正则表达式提取器)和JMeter Perl中的正则表达式(JMeter使用Perl5风格的正则表达式) Use the following Regular Expression: ChemicalID=(\d+) Demo: References: JMeter: Regular Expressions Using RegEx (Regular Expression Extractor) ... -
如果您想按原样使用响应,请添加beanshell-post处理器而不是正则表达式提取器。 vars.put("JSONResponse", prev.getResponseDataAsString()); 现在您可以在下一个请求中使用下面的语句来访问它。 ${JSONResponse} If you want to use response as it is , add a beanshell-post processor instead of Regular Expression Extractor. ...
-
如何在jmeter xpath提取器中从多个soap响应中提取值(how to extract value from multiple soap response in jmeter xpath extractor)[2022-01-07]
用这个: // orderHeader [TASKNAME /文本()= 'TEST_LABEL1'] /的TaskID /文本() Use this: //orderHeader[taskName/text()='TEST_LABEL1']/taskId/text() -
将没有节点本身的xml节点内容提取到jmeter中的变量中(Extract xml node contents without node itself into variable in jmeter)[2023-01-14]
请执行下列操作: 1)像你一样添加XPath Extractor: 参考名称:responseFragment XPath查询:// s:response / s:responseDetails 使用namespaces = true 2)添加Beanshell采样器: String fragment = vars.get("responseFragment"); // Improve this replacement by regexp to avoid side effects String newFr ... -
由于Jmeter中的SAXParseException ,您获得默认值。 您必须选择“使用整洁”选项,如下所示。 使用XPATH以下查询。 //a[@class="link product-link"]/@href You get default value because of SAXParseException in Jmeter. You have to select 'Use Tidy' option as given below. Use below XPATH query. //a[@clas ...