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