Pete Freitag Pete Freitag

How to make ColdFusion MX go to sleep

coldfusion

Many people have had the need for a ColdFusion page to sleep, typically between iterations of a loop. There is a tag called CFX_Sleep in the Tag Gallery, but in ColdFusion MX you don't need a CFX tag to make the current processing thread sleep using the static sleep method on the java.lang.Thread class, part of the standard java platform. Because CFMX doesn't allow us to call a static method without an object reference, we have to first use CreateObject, or CFOBJECT to get an instance of a java.lang.Thread object. We then call the Thread.sleep(long) method, which takes in the number of miliseconds to sleep for.
<cfset thread = CreateObject("java", "java.lang.Thread")>
About to sleep for 5 seconds...<cfflush>
<cfset thread.sleep(5000)>
Done sleeping.


Like this? Follow me ↯

How to make ColdFusion MX go to sleep was first published on October 14, 2002.

If you like reading about thread, cfml, sleep, cfobject, or java then you might also like:

FuseGuard Web App Firewall for ColdFusion

The FuseGuard Web Application Firewall for ColdFusion & CFML is a high performance, customizable engine that blocks various attacks against your ColdFusion applications.

Comments

BTW, this also works on CF5!!!!
by Patrick Steil on 03/08/2004 at 2:06:25 PM UTC
Two words: Thank you.
by Steve Tringali on 06/23/2004 at 11:25:52 AM UTC
I had this error when I patched this code on my template. Pls Help! Unhandled System exception ! Failed to create JavaVM. JNI_CreateJavaVM returned error code JNI_ERR (this may indicate an unrecognized option string)
by Eduard on 07/28/2004 at 6:35:33 AM UTC
neat! We have a large CF app that has recently migrated from CF 5. I'm investigating a duplicate thread problem that has been discovered. The app produces a second thread with the same time stamp in the log. I've seen it with inserts into a database where errors are returned when a key is duplicated by the duplicate thread. Is there any way of catching duplicate threads in CF?
by William Kossack on 04/12/2006 at 2:07:15 PM UTC
This code worked fine when in a CFM to show some text wait 5 seconds and then show some more text. When I moved it to a CFC file the 5 second pause was there but the first set of text did not show up until after the pause. Is this something that can not be done with Components??? Thanks
by Hughes on 06/25/2006 at 12:33:05 PM UTC