Whatever message this page gives is out now! Go check it out!
<!--- 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.sleeptimes++;
}
// 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><!--- Thread1 sleeps to simulate an activity that might hang. --->
<cfthread name="thread1" action="run">
<cfset thread.j=1>
<cfset sleep(50000) >
</cfthread>
<!--- Thread2 loops to simulate an activity that takes less time. --->
<cfthread name="thread2" action="run">
<cfset thread.j=1>
<cfloop index="i" from="1" to="10">
<cfset thread.j=thread.j+1>
</cfloop>
</cfthread>
<!--- The page thread sleeps for 1/2 second to let thread
processing complete. --->
<cfset sleep(500) >
<!--- The page thread loops through the threads and terminates
any that are still running or never started.
Note the use of the cfthread scope and associative array
notation to reference the dynamically named threads without
using the Evaluate function. --->
<cfloop index="k" from="1" to="2">
<cfset theThread=cfthread["thread#k#"]>
<cfif ((theThread.Status IS "RUNNING") || (theThread.Status IS "NOT_STARTED"))>
<cfthread action="terminate" name="thread#k#" />
</cfif>
</cfloop>
<!--- Wait 1/2 second to make ensure the termination completes --->
<cfset sleep(500) >
<!--- Display the thread information. --->
<cfoutput>
thread1 index value: #thread1.j#<br />
thread1 status: #thread1.Status#<br>
thread2 index value: #thread2.j#<br />
thread2 status: #thread2.Status#<br>
</cfoutput><cfthread action="join" name="t1,t2,t3" timeout="6000"/>