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).
- How to make ColdFusion MX go to sleep - October 14, 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 10, 2003
- Getting Size of Heap and Non Heap Memory in CFML - July 17, 2013
- Redirect www and non https in IIS using web.config
- Not authorized to perform: ssm:GetParameters
- What is the difference between ASCII Chr(10) and Chr(13)
- Fixinator and Foundeo Security Bundle
- Running CFML on AWS Lambda with FuseLess Slides
- Updating Java on ColdFusion or Lucee
- ColdFusion returning empty response with server-error: true
- Careful applying CF11u16, CF2016u8, CF2018u2