Viewing by month: May 2013

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 = "";


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