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

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ò)程。
希望這有幫助!
- 2 回答
- 0 關(guān)注
- 182 瀏覽
添加回答
舉報(bào)