title: How can I split a range of values among a "pool" of threads?
body: <p>I have some code that processes around 30,000 records. The basic outline is like this:</p>
  
  <pre><code>startRecordID = 2345;
  endRecordID = 32345;
  for(recordID=startRecordID; recordID &lt;= endRecordID; recordID++){
      // process record...
  }
  </code></pre>
  
  <p>Now, this processing takes a long time, and I'd like to have a thread pool of 15 threads and give each thread a list of recordIDs to process, and then join them all at the end.</p>
  
  <p>In the past I accomplished this with code that looked something like this, where <code>recordLists</code> was an array of sub-arrays each containing 1/15 of the records to be processed:</p>
  
  <pre><code>&lt;cfset numThreads = 15 /&gt;
  &lt;!--- keep a running list of threads so we can join them all at the end ---&gt;
  &lt;cfset threadlist = "" /&gt;
  &lt;cfloop from="1" to="#numThreads#" index="threadNum"&gt;
      &lt;cfset threadName = "recordProcessing_#threadNum#" /&gt;
      &lt;cfset threadlist = listAppend(threadlist, threadName) /&gt;
      &lt;cfthread action="run" name="#threadName#" recordList="#recordList[threadNum]#"&gt;
          &lt;cfloop from="1" to="#ArrayLen(recordList)#" index="recordIndex"&gt;
              &lt;cfset recordID = recordList[recordIndex] /&gt;
              ... process recordID ...
          &lt;/cfloop&gt;
      &lt;/cfthread&gt; 
  &lt;/cfloop&gt;
  &lt;!--- Join all threads before continuing ---&gt;
  &lt;cfthread action="join" name="#threadlist#" timeout="4000"/&gt;
  </code></pre>
  
  <p>This worked well (although I would also convert this old code to cfscript :) ), but to create the recordLists array of sub-arrays is not so simple... The way I can think of to do it would be to loop through the numbers from startRecordID-endRecordID, add each  to an array, then run an ArrayDivide function (that we have already defined in our codebase) on it to split it into numThreads (in this case 15) equal sub-arrays. Considering that I have the start of the range, the end of the range, and the number of threads I want to divide it among, isn't there a simpler way to break it up and assign it to the threads?</p>