开发者

C# -Threading - Execution Order

开发者 https://www.devze.com 2022-12-12 11:27 出处:网络
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).

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).

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. That way your thread execution will be more randomized, but you could even get delay if the ThreadPool is busy.

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.

Also you get different sequence if you call like :(instead of calling after creation)

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号