pf » CFThread - Don't Abuse It
CFThread - Don't Abuse It
I love the fact that you can now create threads with CFML in ColdFusion 8, however as Spiderman can tell you with great power comes great responsibility.
Using threads can increase the performance of your application, however using threads can also decrease performance. And the problem is that CF8 makes it oh so easy to create threads.
One of the potential performance problems you can run into from using threads is called context switching, which can be explained as follows:
- Your boss tells you to work on Project A at 8AM
- At 8:15 your boss tells you you need to work on Project B, so you close all the code you were working on, and open up Project B.
- At 8:25 you need to fix an important bug in project A.
- At 8:30 you get a phone call about Project C, you have to pull up those files.
- Back to project B at 8:35, project A at 8:40, and project D at 8:45
How could you get anything done under the above circumstances? Well computer processors have trouble with that exact problem as well, and they call that context switching. As threads take turns executing their previous state or context must be loaded again when execution of a thread switches between threads. Process and thread scheduling is actually one of the most important parts of an operating system, and includes very well though out algorithms - it's the sort of thing people do their doctoral thesis on. You can read more about it context switching on Wikipedia.
The cost of creating a new thread, is another performance issue associated with using threads. In ColdFusion this is mitigated a bit by using a thread pool, but you can still create performance problems if too much time is spent creating new threads. There is a setting in CF Administrator which limits the number of threads that can be created with CFTHREAD, if this value is reached, the threads will queue up, and wait to execute.
The third issue you may face is shared resource contention. Suppose you have a lot of threads trying to use a shared resource, such as an application variable or file. You can create a bottle neck due to the locking / access synchronization of that resource.
In the end the only way to know if your application will benefit from using threads is to test and tune it under load, which is a something most developers don't do. So I am wondering if we will start to see a lot of performance problems due to overuse of the CFTHREAD tag, what do you think?
add to del.icio.us
| Tags: threading, coldfusion, cfml, cfthread, context switching, threads, coldfusion 8, performance
Related Entries
- 10 Most Useful Image Functions in ColdFusion 8 - April 5, 2008
- CFImage Effects Library for ColdFusion 8 - August 9, 2007
- Serializing CFC's in ColdFusion 8 - August 6, 2007
- CFSCRIPT Cheatsheet - May 5, 2008
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks - April 5, 2008
@Ben - I would think that LOW priority threads would suffer less from context switching, but the extent would probably depend on the thread scheduling algorithm used by the Operating System, and possibly the JVM's threading implementation. Reguardless, though I would say it is probably a really good idea to use a low priority thread for any thread doing a background process that you don't need to wait for.
That brings up an interesting use case for CFTHREAD, suppose you have a scheduled task that runs once a day, it may be a good idea for that task to run in a low priority thread, because otherwise the task will run in a normal priority thread.
I suspect that this may be a contributing factor to only allowing 2 concurrent threads in the Standard License (i.e. programmers with less experience?)
At least that was my understanding...
http://www.adobe.com/products/coldfusion/pdfs/cf8_featurecomp.pdf
Thread pool and thread creation are mutually exclusive...
Lowering thread priority has zero impact on context switching overhead...
And so on, and so on...
- CFSCRIPT Cheatsheet
- 3 New Image Effects for ColdFusion 8
- Googlebot to Submit Web Forms
- ColdFusion 8 Update 1 Fixes some Image Processing Quirks
- 10 Most Useful Image Functions in ColdFusion 8
- Speaking at NYC CFUG This Week
- Adobe AIR Tutorial for HTML / JavaScript Developers
- INFORMATION_SCHEMA Support in MySQL, PostgreSQL
RSS
Pete Freitag is a software engineer, and web developer located in










