Thread Priority, and Yielding

Suppose you have a page that is only run by background processes, or a page that can take a lot of resources, but you don't want it to. Since ColdFusion MX is written in Java, you can access the thread object that your CFML request is running as. Subsequently, you can increase or decrease priority of the thread.
There are a few things to keep in mind when doing this. First, if you decrease the thread priority, you will be tying up the thread for a longer amount of time. There are only a fixed number of threads in the pool, so if you have a lot of concurrent requests, you may not be gaining anything. Also you may be tieing up resources, such as database connections, or shared scope locks (session, application, etc) for longer than necessary, which may cause other pages to wait longer. Second if using this to increase a priority, keep in mind that there may be some overhead involved in changing the thread priority, I haven't done any load testing. And third, you should return the thread to the priority it was set with when your done, because the thread will likely go back into a thread pool, when it is done with your request.
So I'm not promising anything with this technique, weather or not it will do anything helpful is very dependent on your application specifics.
Now let's look at the code:
<cfset thread = CreateObject("java", "java.lang.Thread")>
<cfset thread = thread.currentThread()>
<cfset originalThreadPriority = thread.getPriority()>
<cfset thread.setPriority(thread.MIN_PRIORITY)>
<!--- run your stuff --->
<!--- return thread to its original priority --->
<cfset thread.setPriority(originalThreadPriority)>
Just as I used thread.MIN_PRIORITY to set the thread to the minimum priority, you can use thread.MAX_PRIORITY to set the thread to the maximum priority.
Another handy method in the Thread object is the yield method, which will pause the current thread, and allow other threads to get some work done. Think of it just as a yielding when your driving, it basically lets some cars (threads) go ahead of you, when it's clear you can go ahead.
Here's an example of yielding a thread in cfmx:
<cfset thread = CreateObject("java", "java.lang.Thread")>
<cfset thread = thread.currentThread()>
<cfset thread.yield()>
You may want to use yeild instead of lowering priority, you can call it a few times in your request (just make sure you don't do it while you have resources tied up).
Tweet
Related Entries
- How to make ColdFusion MX go to sleep - October 15, 2002
- Null Java References in CF 6 vs 7 - January 10, 2006
- Checking your JDBC Driver Version - March 31, 2004
- Determining the size of objects in memory - December 11, 2003
- CFML on Google App Engine for Java - April 10, 2009
Trackbacks
Comments
eg: have a thread that sends a response of "record saved" back to the user and spawn a new one to update a slow verity collection (instead of waiting for the collection update before sending the response)?
cheers barry.b
You can do this in CFMX 7 using the asynch event gateway.
>> You can do this in CFMX 7 using the asynch event gateway
well, by gosh, so you can!
enterprise version only it seems, though. We've got software going to 100+ schools on CF7 "standard"
no good, I'm afraid.... POO!
Post a Comment
Recent Entries
- Writing Secure CFML cfObjective 2013 Slides
- Upgrading to Java 7 on Linux
- J2EE Sessions in CF10 Uses Secure Cookies
- Learn about ColdFusion Security at cfObjective 2013
- Session Loss and Session Fixation in ColdFusion
- FuseGuard 2.3 Released
- CKEditor Spell Checker Plugin
- Adobe Says Go Ahead and Upgrade your ColdFusion JVM


add to del.icio.us



