Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at Scotch On The Rock (SOTR) 2010 (London) with:

Sleeping A Thread Pauses The CFSetting Request Time Out

By Ben Nadel on
Tags: ColdFusion

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.



Reader 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...

Reply to this Comment

Ron,

That's interesting. Definitely beyond my level of understanding / visualization abilities... but cool.

Reply to this Comment

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.

Reply to this Comment

@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:

http://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 :)

Reply to this Comment

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.

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.