Below is an update to the original CFTHREAD Proof of Concept (POC) that adds experimental multithreading support to CFML on ColdFusion MX 7.0.2 that Adobe Computer Scientist and ColdFusion Engineering Team developer extraordinaire, Rupesh Kumar put together in his own time for you to play with. Our development engineers are called “Scientists” at Adobe which is cool because it means that we understand the importance of experimentation. And like all good scientists, we hypothesize and test – some things make it into the product, others don’t – but we’re always innovating.This POC works with CF 7.0.2 (Standard or Enterprise). While you can do multi-threading and asynchronous CFML with these POC tags, they don’t provide any of the fine-grained threading control you have with ColdFusion Event Gateways and the Asynchronous Gateway, for example. So you don’t get thread pooling, control over how many worker threads can be active or pooled, metering on Gateway messages In/Out, or any of the other advanced capabilities of the Event Gateway architecture on CF7, but they do the basics: run stuff asynchronously in separate threads and allow for the use of local (thread-local) variables, allow access to shared scopes, etc.
A note on the simple example: you'll want to modify both templates to copy a file (I use a “C:\baseline.txt” file) that exists on YOUR system to a directory that exists on YOUR system (I used “C:\____WORKFOLDER”). I’d recommend turning on debugging so you can see the execution times yourself as well (much more dramatic that way). Also note that spawned threads shouldn't count towards Simultaneous Threads slots of the CF Server, so you shouldn't need to adjust that setting to accommodate your extra threading.
The CFTHREAD POC enables you to spawn a new thread and optionally wait for a thread to finish. A thread can be spawned using
This update will be the last for the CFTHREAD POC, but it does address some of thread safety issues of the first CFTHREAD POC, and has the same syntax. The two small additions to this updated POC are that a) you can pass any variable to the thread tag and b) any variables defined in the thread tag are local to the thread.
Each thread has its own local scope and any variable that you define inside the thread goes into that local scope. This is very much like function local scope except that you don’t need to prefix those variables with 'var'. A page scope or other scope variables will be accessible inside the thread using the appropriate prefix like variables.xx or request.xx. CFTHREAD tag also allows passing any variable as attribute to the tag. The attributes thus passed can be accessed using attributes.xxx.
Apart from thread local scope, there is a special scope for thread called "thread" scope. this is a scope in which only the owner thread can write but all other threads can read values from it. This scope can be accessed by "
A thread can put some data in its scope and it can later be retrieved.
Browser Output and Error Handling
CF code written inside CFTHREAD can not write any content to the browser. So any output written using
Similarly, any error thrown from a
1) To start a thread:
This will spawn a new thread and any content between
2) If you want to wait for this thread to finish, you can use
If thread 'th1' is already over by the time you reach here, it will have no effect. Otherwise it will wait for th1 to complete. One important thing about this POC is that you can wait for only user threads that have been created in that request. So you can not use join for any arbitrary thread in the VM, for example. If you only want to do some operation asynchronously and do not need to wait for its completion, you need not use
3) To retrieve a value put in a particular thread scope, simply access that scope variable. So you can use th1.x to get the value of x kept in th1 scope.
4) CFTHREAD does not provide a way to sleep. however if you want to do it, you can use
This code lets the current thread to sleep for 10 secs (10000 milliseconds). For example, if you want 'th1' of above example to sleep for 10 sec, this code snippet will go inside
CFTHREAD POC Setup
The zip file contains two other files
- cfthread.jar - This file should be kept in
- taglib.cftld - This should be kept in
Restart your server and you should be ready to try out the
DOWNLOAD CFTHREAD POC & SAMPLES ( 101K)
Once again, just to be clear, this POC is not a CF product feature at this time, has not been properly QA'd, contains bugs and is unsupported. There is also little chance that should a POC evolve into a feature that the tag syntax will not change (perhaps drastically) in the future, and there are no plans to update the POC as future major versions of CF come out. CFTHREAD may or may not make it into a future release, but that partly depends on feedback from you, our customers.
Enjoy! And thanks Rupesh!
The agenda for Adobe "MAX 2006" conference at the Venetian Hotel in Las Vegas October 23-26 is posted and registration is open. Go to event page for more info, including pre-conference events, agenda, hotel/travel and registration. A MAX 2006 Attendee FAQ is also available.
For those of you interested in the ColdFusion-related sessions this year I've listed some of them here:
GENERAL AND FULL-DAY SESSIONS:
Architecting ColdFusion Applications for Performance, Scalability, and Maintainability (Full Day Session)
Building Rich Internet Applications with Flex and ColdFusion (Full Day Session)
An Introduction to ColdFusion Powered Flex
Image Manipulation in ColdFusion
Building Your First ColdFusion Application
Building Your First Dynamic Web Application
Advanced ColdFusion Components
Advanced ColdFusion Printing and Reporting
Building ColdFusion Powered Flex Applications
ColdFusion 7 Application Framework
Leveraging ColdFusion Components in ColdFusion
Leveraging the ColdFusion Application Framework
Search Engine Optimization
AJAX/Spry Framework Overview
Developing Hybrid Applications with ColdFusion and Java
The Big Framework and Methodology Debate
Unlocking the ColdFusion Server Black Box
Using ColdFusion Components as Objects
Using ColdFusion with Flex Data Services
For the latest up-to-date information, check out the Sessions by Track page.
Here is the official current listing of actual Adobe ColdFusion Team member blogs, for the record:
ColdFusion Team Member Blogs: