September 13, 2006
Updated CFTHREAD Proof Of Concept for ColdFusion 7.0.2

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 and a thread can join with another thread 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.

Thread Safety
==========

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 ".xxx". The owner thread can also access it using 'thread.xxx'

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 from 'CFTHREAD' will be accessible inside the 'thread' scope using "thread.OUTPUT" or .OUTPUT

Similarly, any error thrown from a body will not be sent to the browser and will be available in thread scope using "thread.ERROR" or .ERROR

Syntax
=====

1) To start a thread:

  
           
           
           
           
           
           
 

This will spawn a new thread and any content between tag will be invoked inside that new thread.

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 /lib/updates
- taglib.cftld - This should be kept in /wwwroot/WEB-INF/cftags/META-INF. To be on the safer side, you should backup your own original copy of this file.

Restart your server and you should be ready to try out the POC!

DOWNLOAD CFTHREAD POC & SAMPLES ( 101K)

Caveats
======

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!

Damon