Learning ColdFusion 8: CFThread Part II - Parallel Threads

<!---
	Build the base URL for the results. This will
	include everything but the start index. We are
	going to be screen-scraping Google for some
	search results.
--->
<cfset strBaseURL = (
	"http://www.google.com/search?" &
	"q=Vin+Diesel" &
	"&num=100" &
	"&start="
	) />
 
 
<!---
	Method Two: Asyncronous parallel thread CFHttp
	calls. This methodology leverages ColdFusion 8's
	new CFThread tag to fire parallel CFHttp calls.
--->
 
 
<!--- Get the starting time. --->
<cfset intStartTime = GetTickCount() />
 
<!---
	Let's get the first 1000 results for Vin Diesel.
	In order to do this, we are going to grab 10 sets
	of 100 results, but this time each grab is going
	to be done in it's own thread.
--->
<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) * 100)#"
			useragent="#CGI.http_user_agent#"
			result="THREAD.Get#intGet#"
			/>
 
	</cfthread>
 
</cfloop>
 
 
<!---
	Now, we have to wait for all of concurrent
	threads to be joined before we can use the
	CFHttp results.
--->
<cfloop
	index="intGet"
	from="1"
	to="10"
	step="1">
 
	<cfthread
		action="join"
		name="objGet#intGet#"
		/>
 
</cfloop>
 
 
<!--- Output retrieval times. --->
<p>
	We Got 1000 Results in
	#NumberFormat(
		((GetTickCount() - intStartTime) / 1000),
		",.00"
		)#
	seconds using CFHttp and CFThread
</p>

For Cut-and-Paste