简体   繁体   English

C#中的线程同步

[英]Thread Synchronization in C#

I have 5 long running process and I need to execute only one Task at a time; 我有5个长期运行的流程,一次只需要执行一个任务即可; I am planning to put them on 5 threads and my only condition is only one thread needs to be exeucuted... 我打算将它们放在5个线程上,而我唯一的条件是只需要执行一个线程即可。

Can you give any example for this ? 你能举个例子吗?

Thanks 谢谢

When you need to execute "one at a time" then do not use more than 1 Thread... 当您需要一次执行一个时,则不要使用多个线程...

Simply execute them in order on 1 Thread. 只需按顺序在1个线程上执行它们即可。

Do I understand you correctly that you want to execute all 5 threads one after the other. 我是否正确理解您要一个接一个地执行所有5个线程。 Like: thread 2 shall only start once thread 1 has finished? 像:线程2仅在线程1完成后才开始?

Then you can have: 然后您可以拥有:

Thread T1 = ...
Thread T2 = ...
Thread T3 = ...
..
Thread T5 = ...

T1.Start();
T1.Join();

T2.Start();
T2.Join();

...

T5.Start();
T5.Join();

But in this case I would advice you to only use 1 thread, that makes things easier. 但是在这种情况下,我建议您仅使用1个线程,这使事情变得更容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM