Skip to main content
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Mark Drew
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Mark Drew

Sleeping A Thread Pauses The CFSetting Request Time Out

By
Published in Comments (5)

This is totally useless information, but I wanted to see what would happen to the page's request time out if you slept the current thread. Here's a little test:

<!--- Set the page time out to only allow for 2 seconds. --->
<cfsetting
	requesttimeout="2"
	showdebugoutput="false"
	/>

<!---
	Create a timer structure to store the start and
	end tick counts of the page processing.
--->
<cfset objTimer = StructNew() />
<cfset objTimer.Start = GetTickCount() />


<!--- Get the current page thread. --->
<cfset objThread = CreateObject(
	"java",
	"java.lang.Thread"
	).CurrentThread()
	/>

<!---
	Get the current thread to sleep for 5 seconds.
	Theoretically, this should force the page to take
	longer to process than the allowable request time out.
--->
<cfset objThread.Sleep(
	JavaCast( "long", (5 * 1000) )
	) />


<!--- Get the end timer tick count. --->
<cfset objTimer.End = GetTickCount() />


<!---
	Output the final message and milliseconds
	that the timer was running for.
--->
Done<br />
Time Elapsed: #(objTimer.End - objTimer.Start)#

The page above should only be allowed to run for 2 seconds. However, half way through, we are grabbing the current thread and getting it to sleep for 5 seconds. This should theoretically make the page take 3 seconds longer than the allowable run time (give or take a few milliseconds). However, when you run the above script, you get this output:

Done
Time Elapsed: 5000

No time out errors were thrown. The tick count successfully records the 5 seconds elapsed, but the time out didn't seem to care. Apparently the sleep request pauses the time out count down.

To those of you who know Java very well, this is probably a "well, duh!!" moment. But me, I don't know Java, so thought this was kind of interesting. Useless.... but interesting.

Want to use code from this post? Check out the license.

Reader Comments

1 Comments

Ben,

New threads are synchronous by default too.

I added this to your code:

<cfset objThread.setName(JavaCast("String","origThread")) />
OrigThread: #objThread.getName()# <br />
<cfset objThread1 = CreateObject ("java", "java.lang.Thread") />
<cfset objThread1.start() />
<cfset objThread1.setName(JavaCast("String","newThread")) />
<cfset ojbThread1.Sleep(JavaCast("long",(5*1000))) />
NewThread: #objThread1.getName()# <br />
OrigThread: #objThread.getName()# <br />

Allen Holub (read: Java Guru) has a 9 part article about Threads and talks alot about asynchronous here: http://www.javaworld.com/javaworld/jw-05-1999/jw-05-toolbox.html?page=1
But it is pretty in-depth...

25 Comments

Your other post on CFThread brought me here..
This is actually incorrect. Sleeping a thread does not pause the request time out.
<cfsetting requesttimeout="2"> will merely override the timeout value set in the admin and this will be in use only when request timeout is enabled.
Ensure that the timeout is enabled and run your example. You should see the result.
And ya, you need to wrap the last line in cfoutput :) so that it produces some output.

Rupesh.
Coldfusion Engineering team.

15,902 Comments

@Rupesh,

I was not even aware that you could turn off request timeout :) All of my pages do run with I think the default 20 seconds or something set in our Admin. I am overriding it in this case with the 2 second timeout. But the point of the post is that the thread sleeps for 5,000 milliseconds. 5 is greater than the requested 2 second timeout... which is why I assume Sleep() pauses the timeout countdown.

Maybe I am not understanding what you are saying.

Also, to be sure that I have request timeout enabled, take a look at this post:

www.bennadel.com/index.cfm?dax=blog:626.view

This uses the request timeout to force a Timeout Exception. All of my testing is done on the same server (except for CF8 testing which is on HostMySite.com).

As for the CFOutput, usually my entire example is wrapped in a CFOutput which I do not include in my demo code because I don't like the extra indent :)

25 Comments

I think this should put the things in perspective.
For timeout, there has to be some one who keeps track whether it is timed out. It is the request thread itself which keeps checking whether it has timed out. If it is timed out, it stops proceeding further and errors out. When you put sleep in the request thread, request thread itself is sleeping and hence it can not check whether it is timed out. The check will happen only after it comes out of the sleep.
The check happens at the start and end of most of the tags/function/iteration.

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel