multithreading - 后台进程是否需要CFThread join?

标签 multithreading coldfusion cfthread

背景:

这是计划作业的一部分,该作业从外部站点检索数据(外部站点提供用于通过 Web 服务检索数据的 API)并使用新信息更新数据库。它正在检索大约 3,500 个数据项。我当前的计划作业会创建一次运行 10 个线程的 CFThread 任务 block ,并在开始下一个 10 个线程 block 之前将它们连接起来。

代码:

<cfset local.nMaxThreadCount = 10>
<!---retrieve a query that contains the items that need to be updated, approximately 3,500 items--->
<cfset local.qryItemsNeedingUpdate = getItemsNeedingUpdate(dtMostRecentItemPriceDate = local.qryMostRecentItemPriceDate.dtMostRecentItemPrice[1])>
<cfset local.nThreadBlocks = Ceiling(local.qryItemsNeedingUpdate.RecordCount / local.nMaxThreadCount)>

<cftry>
<cfloop index="local.nThreadBlock" from="1" to="#local.nThreadBlocks#">
    <cfif local.nThreadBlock EQ local.nThreadBlocks>
        <cfset local.nThreadCount = local.qryItemsNeedingUpdate.RecordCount MOD local.nMaxThreadCount>
    <cfelse>
        <cfset local.nThreadCount = local.nMaxThreadCount>
    </cfif>
    <cfset local.lstThreads = "">
    <cfloop index="local.nThread" from="1" to="#local.nThreadCount#">
        <cfset local.nQryIdx = ((local.nThreadBlock - 1) * local.nMaxThreadCount) + local.nThread>
        <cfset local.vcThreadName = "updateThread#local.qryItemsNeedingUpdate.nItemID[local.nQryIdx]#">
        <cfset local.lstThreads = ListAppend(local.lstThreads, local.vcThreadName)>

        <!---create the attributes struct to pass to a thread--->
        <cfset local.stThread = StructNew()>
        <cfset local.stThread.action = "run">
        <cfset local.stThread.name = local.vcThreadName>
        <cfset local.stThread.nItemID = local.qryItemsNeedingUpdate.nItemID[local.nQryIdx]>

        <!---spawn thread--->
        <cfthread attributecollection="#local.stThread#">
            <cfset updateItemPrices(nItemID = attributes.nItemID)>
        </cfthread>
    </cfloop>

    <!---join threads--->
    <cfthread action="join" name="#local.lstThreads#" />
</cfloop>
    <cfcatch type="any">
<cflog text="detailed error message logged here..." type="Error" file="myDailyJob" application="yes">
    </cfcatch>
</cftry>

问题:

后台进程需要这种逻辑吗?也就是说,是否需要CFThread action="join"?线程中不显示任何内容,并且线程是独立的(不依赖于其他线程或生成它们的进程)。线程更新数据库中的价格并终止。是否有必要限制线程,即一次运行 10 个线程并将它们连接起来?该进程可以循环并一次创建所有 3,500 个线程吗? ColdFusion 会将额外的线程排队并在有时间时运行它们吗?

最佳答案

除非您需要在线程完成后向页面输出信息,否则“join”不是必需的。

线程将排队;这因您运行的 ColdFusion 版本而异。

但是,对于您正在做的事情,线程并不是您想要的。您想要使用消息队列,例如 ActiveMQ 或 Amazon SQS。您可以使用 Adob​​e CF 附带的 ActiveMQ 网关等事件网关,或者如果您使用不同的消息队列或 CF 引擎,则可以编写自己的事件网关。 (例如,我编写了一个使用 Amazon SQS 和 Railo 事件网关的消息系统,用 CFML 编写)

关于multithreading - 后台进程是否需要CFThread join?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14326161/

相关文章:

java - 使用synchronized 会使这段代码有顺序吗?

multithreading - Azure XS 虚拟机上不带 GUI 的 Ubuntu 服务器

data-structures - 停止 ColdFusion 对我的结构/数组进行排序

mysql - 在数据库中存储登录详细信息

冷聚变 : terminate CFTHREAD in separate request

multithreading - CFTHREAD 执行两次

java - 信号量简单示例

c++ - 从线程更新进度条的最佳方法