cfthread tag enables you to create threads, independent streams of code execution, in your ColdFusion application. You use this tag to run or end a thread, temporarily stop thread execution, or join together multiple threads.
For all actions except
cfthread tag must have an empty body and be followed immediately by a
</cfthread> end tag, or must have no end tag and have a slash before the tag closure, as in
<cfthread action="sleep" duration="1000"/>.
You can specify this tag's attributes in an
ColdFusion 8: Added this tag
The action to take, one of the following values:
The number of milliseconds for which to suspend thread processing.
Optional, Required, if
The name of the thread to which the action applies:
The priority level at which to run the thread. The following values are valid:
The number of milliseconds that the current thread waits for the thread or threads being joined to finish. If any thread does not finish by the specified time, the current thread proceeds.
Page-level code (code outside any
cfthread tags) executes in its own thread, referred to as the page thread. Only the page thread can create other threads. A thread that you create cannot create a child thread.
If a thread never completes processing (is hung), it continues to occupy system resources. You can use the ColdFusion Sever Monitor to check for and terminate hung threads.
ColdFusion makes a complete (deep) copy of all the attribute variables before passing them to the thread, so the values of the variables inside the thread are independent of the values of any corresponding variables in other threads, including the page thread. Thus, the values passed to threads are thread safe because the attribute values cannot be changed by any other thread.
Each thread has three special scopes:
- The thread-local scope is an implicit scope that contains variables that are available only to the thread, and exist only for the life of the thread.
- The Thread scope is available to the page and to all other threads started from the page. Its data remains available until the page and all threads started from the page finish, even if the page finishes before the threads complete processing.
The Attributes scope contains attributes that are passed to the scope, and is available only within the thread and only for the life of the thread.
For detailed information about using ColdFusion scopes in threads, see Using ColdFusion Threads in the Developing ColdFusion Applications.All threads in a page share a single Variables scope, so you can use it for data that is common across all threads. You must be careful to lock access to the variables, if necessary, to prevent deadlocks or race conditions between threads.
When ColdFusion uses a connector to access the web server, after the page gets completed, the CGI and Request scopes are not accessible to threads that you create by using the
cfthreadtag. This limitation does not apply if you use the integrated web server or if you run ColdFusion as a J2EE application.
The thread scope contains the following variables that provide information about the thread (metadata):
The amount of processor time that was spent handling the thread.
The structure that is generated if an error occurs during thread execution. The structure contains the keys that you can access in a
The thread name.
Output that is generated by the thread. A thread cannot display output; page-level code must use this variable to display thread results. For detailed information, see Handling thread output in Working with threads in the Developing ColdFusion Applications.
The thread processing priority, as specified in the
The time at which the thread began processing, in ColdFusion date-time format.
The current status of the thread; one of the following values:
The following example uses three threads to get the results of three RSS feeds. The user must submit the form with all three feeds specified. The application joins the threads with a time-out of 6 seconds, and displays the feed titles and the individual item titles as links.