26

As far as Thread Safety goes is this ok to do or do I need to be using a different collection ?

        List<FileMemberEntity> fileInfo = getList(); 

        Parallel.ForEach(fileInfo, fileMember =>
        {
              //Modify each fileMember 
        }

That's not how String.split works.

Trailing empty strings are therefore not included in the resulting array.

http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29

You could check if the string ends in a comma and then add the empty string yourself

4

4 に答える 4

34

メソッドに渡された項目の内容のみを変更する限り、ロックは必要ありません。

(もちろん、リストに重複した参照、つまり同じFileMemberEntityインスタンスへの 2 つの参照がないことを前提としています。)

リスト自体を変更する必要がある場合は、反復可能なコピーを作成し、リストを変更するときにロックを使用します。

List<FileMemberEntity> fileInfo = getList();

List<FileMemberEntity> copy = new List<FileMemberEntity>(fileInfo);
object sync = new Object();

Parallel.ForEach(copy, fileMember => {
  // do something
  lock (sync) {
    // here you can add or remove items from the fileInfo list
  }
  // do something
});
于 2012-06-27T18:10:13.507 に答える
9

You're safe since you are just reading. Just don't modify the list while you are iterating over its items.

于 2012-06-27T17:52:17.077 に答える
1

FileMemberEntityリストを変更していないため、オブジェクトが作用する順序が問題にならない場合は、 を使用できList<T>ます。

ある種の順序を確保する必要がある場合はOrderablePartitioner<T>、基本クラスとして使用して、適切なパーティション分割スキームを実装できます。たとえば、FileMemberEntityに何らかの分類があり、特定の順序で各カテゴリを処理する必要がある場合は、この方法を使用することをお勧めします。

あなたが持っている場合、仮説的に

オブジェクト 1 カテゴリ A

オブジェクト 2 カテゴリ A

オブジェクト 3 カテゴリ B

using を反復する場合、 が処理Object 2 Category Aされる前に が処理されるという保証はありません。Object 3 Category BList<T>Parallel.ForEach

リンク先の MSDN ドキュメントには、その方法の例が示されています。

于 2012-06-27T17:54:13.947 に答える