Thread Priority, and Yielding

April 06, 2005

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

Related: ColdFusion MX Sleep function

Related Entries

3 people found this page useful, what do you think?


this "java.lang.Thread" stuff is interesting! In CF, can you split/create additional threads too? 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
Barry.b, You can do this in CFMX 7 using the asynch event gateway.
thanx Rob >> 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!
can u give when will we go for yeild, wait,sleep methods in java.

Post a Comment


Spell Checker by Foundeo

Recent Entries


did you hack my cf?