This document discusses using ColdFusion threads (<cfThread>) to perform asynchronous tasks and make web applications more responsive. It provides examples of running a single thread asynchronously without waiting for it to finish, as well as running multiple threads in parallel and joining them to retrieve results. Some limitations of ColdFusion threads are that they do not support callbacks, have limited thread hierarchy, and each thread uses a full request. Overall, asynchronous programming with threads can enhance performance by allowing tasks to run concurrently rather than synchronously.
2. Who am I???Billy CravensDeveloping ColdFusion apps since version 4.0Early involvement with .NETConsultant .. Hire me!!!billy@billycravens.com(713) 408-3052
4. Keys to success presentationsKnow your audienceMake your presentation unique and funRelevant, useful examples
5. The unthreaded webRequest Server does its thing ResponseComponents, functions, AJAX, web services, flushing still an HTTP request Wait til its doneSOA: wait on them
9. Some basic examplesSingle service call: overhead + service timeMultiple service calls: overhead + service 1 time + service 2 time + service N time ..
11. Some basic examplesSingle service call: overhead + service timeMultiple service calls: overhead + service 1 time + service 2 time + service N time ..Each service is synchronous wait til previous one finished
12. Types of threading in web appsFor our purposes, 2 types:Send a task away and leave us alonePerform multiple tasks at the same time and wait for results
13. Run off and leave me alone<cfthread action=run name=NameOfThread> <!--- whatever code you want goes here ---></cfthread>
14. Limitations of CFThreadDoesnt support concept of call-backsEither the thread runs and is done when its done with no notification anywhere orParent thread must wait for all child threads to finish and take action then
15. Parallel threads in a pageBasic example: a single thread in a pageWhen we just call a thread without joining it to page level thread, it shoots off into never never landSo to use results of thread in page:At least one thread to executeJoin our threads togetherThread results available via threadName or cfthread structure
17. Caveat (Limitation?)No output! Even if you put output tagsCan access output (and other neat metadata) by cfdumping thread scopeProblem: CFDump will strip out HTMLSolution: just output threadName.outputEspecially if you want to CFDump inside your thread!
18. Multiple threadsSame process, just keep track of your thread namesPass as a list when joiningLoop over cfthread structure to access contents of eachRemember scope:Variables set inside of thread do not affect page-levelRemember to be thread safe! Unpredictable results when you attempt to read variables not local to threadUse attributes scope (remember custom tags?)No control over order data returnedBig question: performance enhanced?
19. Watch out!Other variables: can always write over the variableThreads: must always be named unique!!!!Make unique (using CreateUUID() to be safe)But CFThread scope continues for life of requestLoop over list of thread names instead
20. Other limitationsNo grandparents: can only have one level of child threadsEach thread sucks up a request ??Adobe CF: Standard limited to 10 threads (regardless value set in CF Admin) rest are queuedMust buy EnterpriseBlueDragon and Railo other limitsNot part of application error handlingError: look at threadScope.error structure
21. Favorite tricks: Timeout a code blockSpecify timeout to your page level threadContinues when timeout expiresLook at status to determine if child thread completed (threadName.status)Doesnt kill child thread! Only proceeds without itKill threads in CF Admin or via action=terminate
22. ConclusionSynchronous code forces us to waitAsynchronous code lets us take advantage of more resources, enhancing performanceMust rethink how data is passed around