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
appropriately.
*/
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);
thread
name = threadName
action = "run"
id = i {
//Do some amazing stuff here
sleep(5000);
}
/* If we've reached the number of defined threads, wait until they all finish. */
if(
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 */
thread
action="join"
name="#listingThreadNames#";
/* Clear out the thread names for use in the next iteration of the loop */
listingThreadNames = "";
}
}