作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些代码可以处理大约30,000条记录。基本轮廓是这样的:
startRecordID = 2345;
endRecordID = 32345;
for(recordID=startRecordID; recordID <= endRecordID; recordID++){
// process record...
}
recordLists
是一个子数组数组,每个子数组包含要处理的记录的1/15:
<cfset numThreads = 15 />
<!--- keep a running list of threads so we can join them all at the end --->
<cfset threadlist = "" />
<cfloop from="1" to="#numThreads#" index="threadNum">
<cfset threadName = "recordProcessing_#threadNum#" />
<cfset threadlist = listAppend(threadlist, threadName) />
<cfthread action="run" name="#threadName#" recordList="#recordList[threadNum]#">
<cfloop from="1" to="#ArrayLen(recordList)#" index="recordIndex">
<cfset recordID = recordList[recordIndex] />
... process recordID ...
</cfloop>
</cfthread>
</cfloop>
<!--- Join all threads before continuing --->
<cfthread action="join" name="#threadlist#" timeout="4000"/>
最佳答案
(来自评论..)
如果您已经有了一个数组,为什么还要再次遍历它呢?没有内置函数,但是由于数组是java List
,因此简单的 yourArray.subList(startIndex, endIndex)
就可以解决问题。显然,如果记录数少于处理线程数,则添加一些错误处理。
注意:由于它是一种Java方法,因此索引从零(0)开始,并且endIndex
是排他的。而且,在大多数方面,结果都类似于CF阵列。但是,它是不可变的,即无法修改。
<cfscript>
// calculate how many records to process in each batch
numOfIterations = 15;
totalRecords = arrayLen(recordsArray);
batchSize = ceiling(totalRecords/numOfIterations);
for (t=0; t < numOfIterations; t++) {
// calculate sub array positions
startAt = t * batchSize;
endAt = Min(startAt+batchSize, totalRecords);
// get next batch of records
subArray = recordsArray.subList(startAt, endAt);
// kick off a thread and do whatever you want with the array ...
WriteOutput("<br>Batch ["& t &"] startAt="& startAt &" endAt="& endAt);
}
</cfscript>
关于arrays - 如何在 “pool”线程之间划分值范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28740376/
我是一名优秀的程序员,十分优秀!