Thread Priority, and Yielding
By Pete Freitag
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).
Thread Priority, and Yielding was first published on April 06, 2005.
If you like reading about cfml, java, thread, cfobject, or yield then you might also like:
- How to make ColdFusion MX go to sleep
- Null Java References in CF 6 vs 7
- Checking your JDBC Driver Version
- Determining the size of objects in memory
The Fixinator Code Security Scanner for ColdFusion & CFML is an easy to use security tool that every CF developer can use. It can also easily integrate into CI for automatic scanning on every commit.
Try Fixinator
CFBreak
The weekly newsletter for the CFML Community
Comments
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!
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