<cfthread>
The cfthread tag enables concurrent execution through thread management in your CFML application.
Threads are independent streams of code execution that allow you to perform multiple operations simultaneously, improving application performance and responsiveness. You can use this tag to:
- Create new threads with custom processing
- Join multiple threads together, synchronizing their execution
- Sleep the current thread to pause execution
- Terminate threads that need to be stopped immediately
- Interrupt threads, allowing for cooperative stopping with resource cleanup. (Introduced: 7.0.0.120)
Each thread gets its own isolated variable scope (thread) that persists across the thread's lifetime and can be accessed from other parts of your application.
You can pass in any additional attributes you need to, these are then available within the thread,
in the attributes scope, which is useful for passing data into the thread.
In Lucee these attributes are passed by reference, unlike other CFML engines.
If you are using thread in cfscript, you can also access these via the arguments scope,
but this is not recommended or compatible with other CFML engines.
Each thread has it's own unique set of debugging logs, which will not show up in the main pages, normal debugging report.
You can access this debugging data, inside the thread using <cfadmin action="getDebugData" returnVariable="data">.
This tag may have a body.
This tag is also supported within <cfscript>
<cfthread
action=join|run|sleep|terminate|interrupt
type=daemon|task
retryInterval=any
duration=number
name=string
priority=string
timeout=number
throwOnError=boolean
separateScopes=boolean
><!--- body --->[</cfthread>]
| Attribute | Description | Default |
|---|---|---|
|
action
string, optional
|
edit
Specifies the operation to perform on the thread. Options include:
|
run |
Run Thread
action="run" - Create and execute a new thread with custom processing (supports dynamic attributes)
| Attribute | Description | Default |
|---|---|---|
|
name
string, optional
|
edit
Specifies the target thread identifier(s). The usage depends on the
Thread names should be descriptive of their purpose and follow a consistent naming convention for maintainability. Alias: names |
|
|
type
string, optional
|
edit
Defines the thread execution model:
|
daemon |
|
priority
string, optional
|
edit
Sets the execution priority level for the thread when using
Priority affects thread scheduling by the JVM but does not guarantee execution order. Higher priority threads generally get more processing time than lower priority ones, but this depends on the JVM implementation and system load. Note that page-level code (outside of |
|
|
retryInterval
any, optional
|
edit
When A single rule specifies the interval between retries and the number of retry attempts with a structure containing Example: #{interval:createTimeSpan(0,0,0,5),tries:5}#
Multiple rules can be defined as an array of structures, each containing Example: #[{interval:createTimeSpan(0,0,0,5),tries:5},{interval:createTimeSpan(0,0,0,10),tries:5}]#
Alias: retryintervall |
|
|
separateScopes
boolean, optional
|
edit
Only applicable with
This attribute is useful when you need threads to share state, but requires careful synchronization to avoid data corruption. Introduced: 6.1.0.151 |
Join Threads
action="join" - Synchronize with other threads, waiting for their completion
| Attribute | Description | Default |
|---|---|---|
|
name
string, optional
|
edit
Specifies the target thread identifier(s). The usage depends on the
Thread names should be descriptive of their purpose and follow a consistent naming convention for maintainability. Alias: names |
|
|
timeout
number, optional
|
edit
When using
This attribute prevents deadlocks and allows for graceful timeout handling in multi-threaded operations. When the current thread is the page thread, the page continues waiting until either the threads complete or the timeout expires, regardless of page timeout settings. |
|
|
throwOnError
boolean, optional
|
edit
Only applicable with
This attribute is useful for implementing fail-fast behavior in situations where thread errors should immediately stop dependent operations. Introduced: 6.2.1.24 |
false |
Sleep Thread
action="sleep" - Pause the current thread for a specified duration
| Attribute | Description | Default |
|---|---|---|
|
duration
number, optional
|
edit
The number of milliseconds to suspend thread processing when using Typical use cases include adding delays between operations, implementing rate limiting or throttling, and simulating latency for testing purposes. |
Terminate Thread
action="terminate" - Forcibly stop a thread immediately
| Attribute | Description | Default |
|---|---|---|
|
name
string, optional
|
edit
Specifies the target thread identifier(s). The usage depends on the
Thread names should be descriptive of their purpose and follow a consistent naming convention for maintainability. Alias: names |
Interrupt Thread
action="interrupt" - Request cooperative termination of a thread
| Attribute | Description | Default |
|---|---|---|
|
name
string, optional
|
edit
Specifies the target thread identifier(s). The usage depends on the
Thread names should be descriptive of their purpose and follow a consistent naming convention for maintainability. Alias: names |
Examples
edithttps://www.youtube.com/watch?v=oGUZRrcg9KE
Related System Properties / Environment Variables
- LUCEE_TAG_POPULATE_LOCALSCOPE - Controls whether tags like cflock and cfquery populate their default result variables to local scope when inside a function. When `true`, variables go to local scope. When `false`, restores pre LDEV-5416 behavior where variables go to variables scope
Type: boolean, Default: true, Introduced: 7.0.1.13 - LUCEE_TASKS_LIMIT - Defines the maximum number of elements that can be stored in the cfthread scope. Once this limit is reached, the oldest entries are automatically removed to make room for new ones
Type: numeric, Default: true, Introduced: 6.0.2.10 - LUCEE_THREADS_MAXDEFAULT - Sets the maximum number of parallel threads
Type: numeric, Default: true