第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

使用 Parallel.ForEach 不帶列表

使用 Parallel.ForEach 不帶列表

我想Parallel.ForEach用作一種多線程方法,在沒有列表或文件讀取的情況下執(zhí)行代碼。我想這樣做:Parallel.ForEach{      Console.WriteLine("test");}所以它會(huì)test不停地寫。我將使用一個(gè)IF語(yǔ)句來(lái)檢查它是否應(yīng)該停止或不??梢赃@樣使用Parallel.ForEach嗎?任何幫助,將不勝感激。謝謝!
查看完整描述

2 回答

?
小唯快跑啊

TA貢獻(xiàn)1863條經(jīng)驗(yàn) 獲得超2個(gè)贊

多線程這不會(huì)實(shí)現(xiàn)任何目標(biāo),但如果您堅(jiān)持:


bool _shouldStop { get; set; } = false;

bool _shouldStopSecondThread { get; set; } = false;


public static Main(string[] args)

{


Thread thread = new Thread(print);

Thread anotherThread = new Thread(anotherPrint);

thread.Start();

anotherThread.Start();


//your code here while the worker writes "Test" to console


if(condition) {

   _shouldStop=true;

   _shouldStopSecondThread=false; 

}

}    

//...


public void print()

{

   while (!_shouldStop)

   {

       Console.WriteLine("test");

   }

   Console.WriteLine("worker terminated");

}


public void anotherPrint()

{

   while (!_shouldStopSecondThread)

   {

       Console.WriteLine("test");

   }

   Console.WriteLine("worker terminated");

}


查看完整回答
反對(duì) 回復(fù) 2021-11-28
?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

您想要的不是很清楚,但您可以嘗試這種方法:


        var parallelDrege = Environment.ProcessorCount;

        while (true)

        {

            Parallel.For(0, parallelDrege, t =>

            {

                Console.WriteLine("test");

            });

        }

將 parallelDegre 設(shè)置為所需的值,并注意每個(gè)批次都將并行處理,但批次之間是一個(gè)連續(xù)的過(guò)程。


希望這有幫助!


查看完整回答
反對(duì) 回復(fù) 2021-11-28
  • 2 回答
  • 0 關(guān)注
  • 182 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)