Description

Causes the current thread to stop processing for a specified period of time.

Returns

Does not return a value.

Category

System functions

Syntax

Sleep(duration)

See also

cfthreadUsing ColdFusion Threads in the Developing ColdFusion Applications

History

ColdFusion 8: Added function

Parameters

Parameter

Description

duration

Time, in milliseconds, to stop processing the thread.

Description

The Sleep function is useful when one thread must wait until another thread performs some action. The thread that must wait uses the Sleep function to stop processing for a time, and, when it awakens, checks to see if the other thread is ready. If it is not, the thread can sleep again. This type of action is useful, for example, when one thread must wait for another thread to complete initialization operations that apply to both threads.The Sleep function behaves identically to the cfthread tag with an action attribute value of sleep.

Example

The following example has two threads. The second thread (threadB) uses the sleep function to ensure that the first thread (threadA) has completed before it starts processing.

<!--- ThreadA loops to simulate an initialization activity that might take time. --->
<cfthread name="threadA" action="run">
<cfset thread.j=1>
<cfloop index="i" from="1" to="99999">
<cfset thread.j=thread.j+1>
</cfloop>
</cfthread>

<!--- ThreadB loops while threadA is not finished, sleeping for
1/2 second each time. --->
<cfthread name="threadB" action="run">
<cfscript>
thread.sleepTimes=0;
thread.initialized=false;
while ((threadA.Status != "COMPLETED") && (threadA.Status
!= "TERMINATED")) {
sleep(500);
thread.sleeptimes++;
}
// Only do the post-initilization code if the threadA completed.
If (threadA.Status == "COMPLETED") {
thread.initialized=true;
// Post-initialization code would go here.
}
</cfscript>
</cfthread>

<!Join the threads. --->
<cfthread action="join" name="threadA,threadB" timeout="10000"/>

<!--- Display the thread information. --->
<!--- Different actions might be taken based on the thread status information. --->
<cfoutput>
threadA index value: #threadA.j#<br />
threadA status: #threadA.Status#<br>
threadB status: #threadB.Status#<br>
threadB sleepTimes: #threadB.sleepTimes#<br>
threadB initialized: #threadB.initialized#<br>
</cfoutput>

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy