我目前正在努力掌握线程。我有一种感觉,这可能与我的瞄准镜有关;然而,我看不出这件事哪里不对。
我的CFC包含以下功能:
<cfcomponent output="false" hint="thread stuff.">
<cffunction name="threadTest" access="public" returntype="struct">
<cfscript>
local.lstOne = "1,2,3,4,5,6";
local.a = [];
local.s = {};
local.lst = "";
for(local.x = 1; local.x lte listlen(local.lstOne,','); local.x++){
local.lst &= (len(local.lst) gt 0) ? ',thr#local.x#' : 'thr#local.x#';
thread action="run" name="thr#local.x#" nIndex="#local.x#" aArray="#local.a#"{
thread.y = attributes.nIndex;
thread.aArray = attributes.aArray;
if(thread.y mod 2){
thread.c = 1;
} else {
thread.c = 0;
}
thread.stArgs = {};
thread.stArgs.nMod = thread.c;
arrayAppend(thread.aArray, thread.stArgs);
}
}
threadJoin(local.lst);
local.s.counts = local.a;
return local.s;
</cfscript>
</cffunction>
</cfcomponent>
我有一个CFM页面,看起来有点像:
<cfscript>
theThread = createObject( "component", "ThreadStuff" ).init();
theThread.threadTest();
</cfscript>
当我运行这个程序时,coldfusion返回错误元素X在LOCAL中未定义。
我不知道为什么在循环的第一次迭代后,它会丢失local.x(我已经通过在循环开始和结束时进行转储来证明这一点,但它无法到达local.x=2)。
我哪里可能出错了?
Coldfusion 9.0.0(本问题中使用的版本:9,0,0251028)有一个错误,当在函数的循环中使用线程时,本地作用域会中断。
此问题已在Coldfusion 9.0.1中修复,请参阅此处的详细信息:http://helpx.adobe.com/coldfusion/kb/issues-fixed-coldfusion-9-0.htmlid:80153。
如果问题是变量local.x没有递增,请首先注释掉所有线程内容。将其替换为本地的写转储。在循环之前和之后写入转储本地作用域。
一旦获得local.x增量,就添加空线程。继续对本地作用域进行写转储,以便查看这是否是导致问题的原因。如果local.x仍在递增,请添加非常小的代码位,直到找到导致问题的位。