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  |  Print Page



Learning ColdFusion 9 - ColdFusion 9 tutorials, samples, examples, demos

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 »
7,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 »
7,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
Mar 16, 2010 at 9:07 PM
The Wrestler Starring Mickey Rourke And Marisa Tomei
Rowdy Roddy Piper too. Did you ever see "They Live!" ? His movie claim to fame. No dialogue, but a good cult favorite. Not a bad sci-fi. Wrestlmania and rock n roll --- when I had a clear head. H ... read »
Mar 16, 2010 at 8:54 PM
The Wrestler Starring Mickey Rourke And Marisa Tomei
I remember ALL those guys, remember the Warroir too And Randy Savage. Wm 1V Wow never got an offer to watch that with anyone before lol well I aint sure it is possible, but if it was, sure. Old sc ... read »
Mar 16, 2010 at 7:39 PM
The Wrestler Starring Mickey Rourke And Marisa Tomei
@Patricia, Circus life indeed. I go back to the Bruno Sammartino days and even a few of those oldies died from heart related issues. Gorilla Monsoon, Big John Studd, Andre of course, Big Bossman... ... read »
Mar 16, 2010 at 7:08 PM
A Little Christmas Female Muscle
guauuuuuuuuu. hermosa ... read »
Mar 16, 2010 at 6:23 PM
The Wrestler Starring Mickey Rourke And Marisa Tomei
yep a huge gap and true dat it wasn't as odveus as it is now, now it's just dumb and I HATE all the soap crap they toss in. I stopped watching as soon as Undertaker started talking. The man's mouth ... read »
Mar 16, 2010 at 5:37 PM
Managing ColdFusion Sessions In A ColdFusion Builder Extension
@Papichulu, I hope you're getting some value out of it :) Step on in if you think of anything - more than welcome. ... read »
Mar 16, 2010 at 5:15 PM
Managing ColdFusion Sessions In A ColdFusion Builder Extension
I feel like you two are having a conversation at the kitchen table and I'm outside listening through an open window! ... read »
Mar 16, 2010 at 4:38 PM
The Wrestler Starring Mickey Rourke And Marisa Tomei
@Patricia, Boy there was a biggggg gap between posts on this one Pat! Just take a look at the history of these guys. So many dead and short lives. Jake the Snake, The Junk Yard Dog, Ravishing Rick ... read »