Bless Yahu
Bless Yahu

Reputation: 1341

Using thread.join to make sure all threads in a collection execute before moving on

Is there a problem with this type of implementation to wait for a batch of threads to complete before moving on, given the following circumstances?:

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;
}

Upvotes: 0

Views: 759

Answers (2)

Richard Szalay
Richard Szalay

Reputation: 84734

The waiting implementation seems fine, just be sure that CloneCustomerPrices is thread safe.

Upvotes: 1

Adam A
Adam A

Reputation: 14618

Makes sense to me, so long as the threads finish by the timeout. Not sure what newCustomer is (same as cust?). If that's the case I also don't know how to plan to return just one of them.

Upvotes: 1

Related Questions