Easy Thread Throttling in ColdFusion

I do a lot of large processing tasks at LandsofAmerica, and in order to get these scheduled operations to complete in any reasonable amount of time I use the <cfthread> tag to spin off multiple processing threads at a time. Unfortunately, some of these threads take longer to complete than it does to spin up another 5,000 threads in the queue. Once you break that thread limit, ColdFusion to stop accepting new threads and will throw an exception. So I use the method below to make sure that I don't spin up too many threads at one time.

So the trick is to create each thread with a unique name, keep track of those names, and wait for each batch of threads to finish before starting the next batch. And here's your sample code:

	We need to keep track of the names of our threads
	in order to make sure that we limit our processing
listingThreadNames = "";

	And just for fun, we'll create a variable to keep
	track of how many threads we want to spin off at any
	one time.
numberOfThreads = 10;

/* Loop and start creating threads */
for(i = 1; i LTE 100; i++){
	Create a thread name that we'll use to create
	the thread and keep track of it. Make sure that
	this is unique, or you'll get errors with duplicate
	thread names
	threadName = "MyThread_#i#";
	listingThreadNames = ListAppend(listingThreadNames, threadName);

		name = threadName
		action = "run"
		id = i {
			//Do some amazing stuff here

	/* If we've reached the number of defined threads, wait until they all finish. */
		listingThreadNames NEQ ""
		AND ListLen(listingThreadNames) EQ numberofThreads
		writeOutput("Waiting on the following threads to finish: #listingThreadNames#<hr />");
		/* Join up the threads, which will cause a pause until all the threads are done */

		/* Clear out the thread names for use in the next iteration of the loop */
		listingThreadNames = "";


Posted by Daniel Short on May 10, 2013 at 9:55 AM | Categories: ColdFusion -



Bill wrote on 05/10/13 10:47 AM

good stuff. thanks for sharing.
Adam Tuttle

Adam Tuttle wrote on 05/11/13 6:50 AM

I recommend you take a look at CFConcurrent. There's a CF9 bug that causes a memory leak when you use CFConcurrent there, but if your use is infrequent or isolated (e.g. you can restart the instance after the work is complete), or if you're on CF10, then it really is a superior way to do threading.