The following are best-practice techniques for generating and controlling ColdFusion threads. Add these to your coding methodology to ensure better performance and thread safeness.
Do not overuse ColdFusion threads; performance gains may be realized by leveraging the ColdFusion CFML gateway for some tasks.
Keep thread names unique.
Always specify a timeout when joining threads. Specify a timeout value that is less than the ColdFusion Administrator Timeout Request setting when joining threads to the page-level request.
Properly scope all variables inside the thread body. Remember that unscoped variables default to the THREAD-LOCAL scope and are not accessible outside the thread.
Use the thread’s name to access THREAD scope variables. Use CFTHREAD when accessing dynamically named threads.
Use <cflock> to prevent potential deadlocks and race conditions between threads. Use the proper scope locks when threads access shared server resources: Server-, Application-, or Session-scoped variables. Use a Request scope lock when threads modify variables or Request-scoped variables. Use named locks when threads access shared resources such as Microsoft Exchange Server or FTP connections.
Use the cfthread output metadata variable in page-level code to display thread-generated text.
Use <cfabort>, <cfrethrow>, or <cfthrow> to send thread-level exceptions to the page-level code and properly set the threadName.Error and threadName.Status metadata variables.
Use the theadName.Error metadata variable to handle thread-specific exceptions in external threads.
Use <cflog> and <cftrace> to help track threads.
Use the ColdFusion Administrator’s Maximum Number of Threads Available for CFTHREAD setting to control the pool size for concurrently running ColdFusion threads.
Use the ColdFusion Server Monitor or the Server Monitor API to monitor and terminate hung threads.
You are currently reading a PREVIEW of this book.
Get instant access to over
$1 million worth of books and videos.