使用cfthread join获取在cfloop中运行的变量的值

Dee*_*dav 5 coldfusion cfthread cfloop

感谢回复!!但我仍然无法做到这一点.我得到的错误是"类型objGet1在类型类coldfusion.runtime.VariableScope类型的Java对象中未定义."

以下是我的完整代码.我只想转储包含cfhttp信息的每个线程的值.

http://www.google.com/search?"&"q = Vin + Diesel"&"&num = 10"&"&start =")/>

<cfset intStartTime = GetTickCount() />

<cfloop index="intGet" from="1" to="10" step="1">

    <!--- Start a new thread for this CFHttp call. --->
    <cfthread action="run" name="objGet#intGet#">

        <cfhttp method="GET" url="#strBaseURL##((intGet - 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGet#" />

    </cfthread>

</cfloop>

<cfloop index="intGet" from="1" to="10" step="1">

    <cfthread action="join" name="objGet#intGet#" />
    <cfdump var="#Variables['objGet'&intGet]#"><br />

</cfloop>
Run Code Online (Sandbox Code Playgroud)

当我在循环中加入线程后使用.我得到了预期的结果谢谢!!

Ant*_*ony 6

这里发生了两个问题.

正如Zugwalt所指出的,您需要在线程范围内显式传入要引用的变量.他错过了CGI变量,你的线程中不存在该范围.所以我们传入了我们需要在线程中使用的东西,userAgent,strBaseURL和intGet.

第二个问题,一旦加入,你的线程不在变量范围内,它们在cfthread范围内,所以我们必须从那里读取它们.

更正代码:

<cfloop index="intGet" from="1" to="2" step="1">

    <!--- Start a new thread for this CFHttp call. Pass in user Agent, strBaseURL, and intGet --->
    <cfthread action="run" name="objGet#intGet#" userAgent="#cgi.http_user_agent#" intGet="#intGet#" strBaseURL="#strBaseURL#">

        <!--- Store the http request into the thread scope, so it will be visible after joining--->
        <cfhttp method="GET" url="#strBaseURL & ((intGet - 1) * 10)#" userAgent="#userAgent#" result="thread.get#intGet#"  />

    </cfthread>

</cfloop>

<cfloop index="intGet" from="1" to="2" step="1">

    <!--- Join each thread ---> 
    <cfthread action="join" name="objGet#intGet#" />
    <!--- Dump each named thread from the cfthread scope --->
    <cfdump var="#cfthread['objGet#intGet#']#" />

</cfloop>
Run Code Online (Sandbox Code Playgroud)