4

如何在 Parallel.Foreach 中定义计数器并以特定数量停止该战利品
我问这个问题是因为 Parallel.ForEach 中的计数器在常规操作中不起作用。
请看这个小例子:

static void Main(string[] args)
{
    int Count_Step = -1;
    string[] lines = new string[] 
    { 
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        "10",
        "11",
        "12",
        "13",
        "14",
        "15",
        "16",
        "17",
        "18",
        "19"
    };

    List<string> list_lines = new List<string>(lines);

    ParallelOptions parallelOptions = new ParallelOptions();
    parallelOptions.MaxDegreeOfParallelism = 3;

    Parallel.ForEach(list_lines, parallelOptions, (line, state, index) =>
    {
        if (Count_Step == 10)
            state.Stop();
        Count_Step++;
        Console.WriteLine(index + " : " + line + " : " + Count_Step);
        //Thread.Sleep(5000);
    });

    Console.ReadLine();
}

我想要输出 10 行,而不是更多!
我怎样才能做到这一点?

提前致谢

4

2 回答 2

11

如果您只想输出 10 行,请改为执行此操作,

static void Main(string[] args)
{
    var lines = new List<string> 
    { 
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        ...
    };

    var parallelOptions = new ParallelOptions
        {        
            MaxDegreeOfParallelism = 3
        };

    Parallel.ForEach(lines.Take(10), parallelOptions, (line, index) =>
    {
        Console.WriteLine("{0} : {1}", index, line);
        ////Thread.Sleep(5000);
    });

    Console.ReadLine();
}

或者

static void Main(string[] args)
{
    var lines = new List<string> 
    { 
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        ...
    };

    var parallelOptions = new ParallelOptions
        {        
            MaxDegreeOfParallelism = 3
        };

    Parallel.For(0, 9, parallelOptions, i =>
    {
        Console.WriteLine("{0} : {1}", i, lines[i]);
        ////Thread.Sleep(5000);
    });

    Console.ReadLine();
}

甚至

static void Main(string[] args)
{
    var lines = new List<string> 
    { 
        "0",
        "1",
        "2",
        "3",
        "4",
        "5",
        "6",
        "7",
        "8",
        "9",
        ...
    };

    Enumerable.Range(0, 10).AsParallel().WithDegreeOfParallelism(3).ForAll(i =>
    {
        Console.WriteLine("{0} : {1}", i, lines[i]);
        ////Thread.Sleep(5000);
    }

    Console.ReadLine();
}

如果您真的希望您的并行迭代更新循环外的某些值,那么您将需要确保更改变量的所有更新和读取都是线程安全的。

如果你要增加一个整数,你可以这样做。

var stepCount = 0;

Enumerable.Range(0, 10).AsParallel().WithDegreeOfParallelism(3).ForAll(i =>
    {
        var thisCount = Interlocked.Increment(ref stepCount);
        Console.WriteLine("{0} : {1} : {2}", i, lines[i], thisCount);
        ////Thread.Sleep(5000);
    }

Console.ReadLine();

但是,thisCount控制台窗口中的输出并不保证是连续的,两行之间可能存在线程切换。

如果你想做一些更复杂的事情,比如取消部分处理,你应该看看BlockingCollection

于 2012-09-25T13:44:02.287 回答
4

尝试Parallel.For()

Parallel.For 方法

于 2012-09-25T13:47:49.313 回答