首页 文章

使用thread.join确保集合中的所有线程在继续之前执行

提问于
浏览
0

考虑到以下情况,在继续之前等待一批线程完成这种类型的实现是否存在问题?:

  • 无法使用CCR或PFX .

  • Customer.Prices集合和newCustomer未被变异 .

  • CloneCustomerPrices将Customer.Prices集合中的每个价格执行深层复制到新的价格集合中 .

public List[Customer] ProcessCustomersPrices(List [Customer] Customers)
{
[Code to check Customers and  deep copy Cust data into newCustomers]
List[Thread] ThreadList = new List[Thread]();
foreach(Customer cust in Customers)
{
ThreadList.Add(new Thread(() => CloneCustomerPrices(cust.Prices, newCustomer)));
}

Action runThreadBatch = () =>
            {
                 ThreadList.ForEach(t => t.Start());
                 ThreadList.All    (t => t.Join([TimeOutNumber]));
            };

runThreadBatch(CopyPriceModelsCallback, null);

[More Processing]
return newCustomers;
}

2 回答

  • 1

    等待实现似乎很好,只需确保CloneCustomerPrices是线程安全的 .

  • 1

    只要线程在超时结束时对我有意义 . 不确定newCustomer是什么(与cust相同) . 如果是这种情况我也不知道如何计划只返回其中一个 .

相关问题