感谢您的回复!但我还是做不到。我得到的错误是"Element objGet1 is undefined in a Java object of type coldfusion.runtime.VariableScope“。
下面是我的完整代码。我只想转储包含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>当我在循环中使用after线程连接时。我得到了想要的结果,谢谢!
发布于 2010-07-16 02:38:30
这里发生了两个问题。
正如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>发布于 2010-07-15 01:11:33
通常,未限定作用域的变量被放入Variables作用域,因此您可以使用结构括号表示法来引用它们:
Variables['objGet#intGet#']或
Variables['objGet'&intGet]它们基本上都在做同样的事情--只是语法不同而已。
发布于 2010-07-16 02:01:01
在cfthread标记内运行的代码有其自己的作用域。尝试将您希望其访问的变量作为属性进行传递。我喜欢给它起不同的名字,只是为了帮助我保持跟踪。
<!--- Start a new thread for this CFHttp call. --->
<cfthread action="run" name="objGet#intGet#" intGetForThread="#intGet#">
<cfhttp method="GET" url="#strBaseURL##((intGetForThread- 1) * 10)#" useragent="#CGI.http_user_agent#" result="THREAD.Get#intGetForThread#" />
</cfthread>
https://stackoverflow.com/questions/3248513
复制相似问题