
330
DEVELOPING
COLDFUSION 9 APPLICATIONS
Developing CFML Applications
Las
t
upda
te
d 8/5/2010
<!--- ThreadA loops to simulate an activity that might take time. --->
<cfthread name="threadA" action="run">
<cfset thread.j=1>
<cfloop index="i" from="1" to="1000000">
<cfset thread.j=thread.j+1>
</cfloop>
</cfthread>
<!--- ThreadB loops, waiting until threadA finishes looping 40000 times.
the loop code sleeps 1/2 second each time. --->
<cfthread name="threadB" action="run">
<cfscript>
thread.sleepTimes=0;
thread.initialized=false;
while ((threadA.Status != "TERMINATED") && (threadA.j < 400000)) {
sleep(500);
thread.sle+;
}
// Don't continue processing if threadA terminated abnormally.
If (threadA.Status != "TERMINATED") {
thread.initialized=true;
// Do additional processing here.
}
</cfscript>
</cfthread>
<!Join the page thread to thread B. Don't join to thread A.--->
<cfthread action="join" name="threadB" timeout="10000" />
<!--- Display the thread information. --->
<cfoutput>
current threadA index value: #threadA.j#<br />
threadA status: #threadA.Status#<br>
threadB status: #threadB.Status#<br>
threadB sleepTimes: #threadB.sleepTimes#<br>
Is threadB initialized: #threadB.initialized#<br>
</cfoutput>
Ending a thread
If a thread never completes processing (is hung), it continues to occupy system resources, so it is good practice to have
your application check for hung threads and end them. Also consider ending threads that take excessive time to
process and might significantly reduce the responsiveness of your application or server.
To end a thread, use the
cfthread
tag with an
action
attribute value of
terminate
, as the following code snippet
shows.