2 回答
TA贡献1863条经验 获得超2个赞
多线程这不会实现任何目标,但如果您坚持:
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贡献1893条经验 获得超10个赞
您想要的不是很清楚,但您可以尝试这种方法:
var parallelDrege = Environment.ProcessorCount;
while (true)
{
Parallel.For(0, parallelDrege, t =>
{
Console.WriteLine("test");
});
}
将 parallelDegre 设置为所需的值,并注意每个批次都将并行处理,但批次之间是一个连续的过程。
希望这有帮助!
- 2 回答
- 0 关注
- 156 浏览
添加回答
举报