Sleeping A Thread Pauses The CFSetting Request Time Out

Posted February 23, 2007 at 9:26 AM

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:

 Launch code in new window » Download code as text file »

  • <!--- 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.

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Permalink  |  Print Page




Reader Comments

Feb 23, 2007 at 12:13 PM // reply »
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...


Feb 23, 2007 at 1:27 PM // reply »
6,516 Comments

Ron,

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


Jun 1, 2007 at 1:50 PM // reply »
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.


Jun 1, 2007 at 2:26 PM // reply »
6,516 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:

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


Jun 1, 2007 at 3:06 PM // reply »
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.


Post Comment  |  Ask Ben

Recent Blog Comments
Nov 22, 2009 at 4:30 AM
jQuery Live() Method And Event Bubbling
dasegtezr ... read »
Nov 22, 2009 at 4:03 AM
jQuery Live() Method And Event Bubbling
C_fieri ... read »
Nov 22, 2009 at 1:56 AM
Learning ColdFusion 9: Using CFQuery In CFScript Can Enable SQL Injection Attacks
Why adobe would give you script equivalent of cfquery is beyond me. I love cfquery tag because it helps me wriite clean sql, and get away from the horrible jdbc queries If I wanted to write javali ... read »
Nov 22, 2009 at 1:45 AM
Streaming Text Using ColdFusion's CFContent Tag And The Variable Attribute
The reason you would want to do this is to stream. Ack json/xml files to ria clients I used thus technique before because putting json in response stream causes debugging info to come thru As well a ... read »
Nov 21, 2009 at 6:47 PM
Hal Helms - Real World Object Oriented Development, Sarasota - Day Five
@charlie griefer, Thank you.. ... read »
Nov 21, 2009 at 5:15 PM
Using ColdFusion Structures To Remove Duplicate List Values
@Jose Galdamez, Oh heh yeah I didn't paste the whole code. I should have defined the vars -- my bad. It's fixed thou. Thanks. ... read »
Nov 21, 2009 at 4:49 PM
Styling The ColdFusion 8 WriteToBrowser CFImage Output
Great work yet again Ben! Whilst I didn't use this whole code, I copied some of your regex code for a similar problem with the lack of an alt attribute and unescaped ampersands in CFIMAGE for Railo 3 ... read »
Nov 21, 2009 at 1:13 PM
My First ColdFusion Builder Extension - Encrypting And Decrypting CFM / CFC Files
@Ben, Because I am pedantic, I just want to make sure that everyone knows there is absolutely no encryption going on. There is only encoding and obfuscation. The cfencode tool only obfuscates your C ... read »