Sleeping A Thread Pauses The CFSetting Request Time Out

Posted February 23, 2007 at 9:26 AM by Ben Nadel

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

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 »
11,238 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 »
11,238 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 A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
May 14, 2013 at 6:57 PM
The UX Of Prototyping: Low-Fidelity Is The New High-Fidelity
@Phillip, I'm not sure I follow what you mean? Are you saying that you looked at the list of widgets provided by the jQuery UI and let that be your style guide? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools