Adobe COLDFUSION 9 Manual page 336

Developing applications
Hide thumbs Also See for COLDFUSION 9:
Table of Contents

Advertisement

DEVELOPING COLDFUSION 9 APPLICATIONS
Developing CFML Applications
<!--- 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>
Note: You can also have the ColdFusion Sever Monitor automatically check for and terminate hung threads.
Joining threads
You use the
with an
cfthreadtag
one thread depends on one or more other threads completing before it can do some processing. For example, a page
can start multiple threads to do processing and join them before it processes the thread results. By default, the
action stops the current thread from doing further processing until all the specified threads complete processing.
You can use a
attribute to specify the number of milliseconds that the current thread waits for the thread or
timeout
threads being joined to finish. If any thread does not finish by the specified time, the current thread proceeds without
waiting for the remaining thread or threads to complete.
The following code, for example, joins three threads to the current thread (often, the main page thread). The current
thread waits up to six seconds for the other threads to complete, and continues processing if one or more threads do
not complete by then.
attribute value of
action
join
Last updated 8/5/2010
to join two or more threads. You join threads when
331
join

Advertisement

Table of Contents
loading

Table of Contents