简体   繁体   English

C#-Threading - 执行顺序

[英]C# -Threading - Execution Order

When I execute threads th1,th2 and th3.They are executing one after another.How do i change my code so that the execution order is not predictable .(Without using Random). 当我执行线程th1,th2和th3时,它们一个接一个地执行。如何更改我的代码,以便执行顺序不可预测。(不使用Random)。

public class Test
{
    static void Main()
    {

        Person p = new Person();
        p.Id = "cs0001";
        p.Name = "William";

        Thread th1 = new Thread(()=>ProcessOne(p));
        th1.Name = "ThreadOne";
        th1.Start();

        Thread th2 = new Thread(()=>ProcessTwo(p));
        th2.Name = "ThreadTwo";
        th2.Start();

        Thread th3 = new Thread(()=> ProcessThree(p));
        th3.Name = "ThreadThree";
        th3.Start();

        Console.ReadKey(true);
    }

    static void ProcessOne(Person p)
    {
         Console.WriteLine("Thread {0} is executing", 
         Thread.CurrentThread.Name);
          Console.WriteLine("Id :{0},Name :{1}", p.Id, p.Name);
    }

    static void ProcessTwo(Person p)
    {
        Console.WriteLine("Thread {0} is executing",
        Thread.CurrentThread.Name);
        Console.WriteLine("Id :{0},Name :{1}", p.Id, p.Name);
    }

    static void ProcessThree(Person p)
    {
        Console.WriteLine("Thread {0} is executing",
        Thread.CurrentThread.Name); 
        Console.WriteLine("Id :{0},Name :{1}", p.Id, p.Name);
    }
}

public class Person
{

    public string Id
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }

}

In your code the order of execution is not predictable. 在您的代码中,执行顺序是不可预测的。 The reason you see sequential calls is because your methods do very little work and by the time you start the next thread the first already finished. 您看到顺序调用的原因是因为您的方法执行的工作很少,并且当您启动第一个已完成的下一个线程时。 By the way when I ran your code I got this result: 顺便说一句,当我运行你的代码时,我得到了这个结果:

Thread ThreadOne is executing
Thread ThreadTwo is executing
Id :cs0001,Name :William
Thread ThreadThree is executing
Id :cs0001,Name :William
Id :cs0001,Name :William

One can clearly see that the methods execute in parallel. 可以清楚地看到方法并行执行。 Running the program multiple times you will get different results. 多次运行程序会得到不同的结果。

If your threads run long enough they will start to execute in more random order. 如果你的线程运行得足够长,它们将开始以更随机的顺序执行。 However if the thread terminates very quickly epically while you are debugging this deterministic behavior would be expected. 但是,如果线程在调试时非常快速地终止,则可以预期这种确定性行为。

You could have attempted access to a shared resource and cause the treads to lock hold and what depending on which thread can obtain the lock. 您可能已尝试访问共享资源并导致踏板锁定保持以及取决于哪个线程可以获取锁定。

Instead of manually starting new threads, you could let handle the ThreadPool class the handling of threads. 您可以让ThreadPool类处理线程,而不是手动启动新线程。 That way your thread execution will be more randomized, but you could even get delay if the ThreadPool is busy. 这样你的线程执行会更随机,但如果ThreadPool很忙,你甚至可能会遇到延迟。

ThreadPool.QueueUserWorkItem(() => ProcessOne(p));

May be insert different delays using Thread.Sleep(time) in each method,assigning different time durations to sleep().delay will simulate work being done. 可以在每个方法中使用Thread.Sleep(time)插入不同的延迟,为sleep()分配不同的持续时间.delay将模拟正在完成的工作。

Also you get different sequence if you call like :(instead of calling after creation) 如果你这样调用你也会得到不同的顺序:(而不是在创建后调用)

th1.Start();
th2.Start();
th3.Start();

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

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