using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
namespace ConsoleApplication58
{
class MultiThreading
{
static void Main(string[] args)
{
MultiThreading mul = new MultiThreading();
Thread t1 = new Thread(new ThreadStart(mul.WriteX));
Thread t2 = new Thread(new ThreadStart(mul.WriteO));
t1.Priority = ThreadPriority.Lowest;
t2.Priority = ThreadPriority.Highest;
t1.Start();
t2.Start();
}
private void WriteX()
{
for (int i = 0; i < 300; i++)
{
Console.Write("X");
}
}
private void WriteO()
{
for (int i = 0; i < 300; i++)
{
Console.Write("O");
}
}
}
}
When I execute above code, I expect X's end of the printing job because I gave that method lowest priority but sometimes I get O's at the end. I mean doesn't giving high priority to 2nd thread guarantee it will finish sooner?
There is no practical guarantee about thread scheduling induced by the priority setting.
For example, the high priority thread could block on IO or a page fault. Then, another thread can execute.
This is not a good way to synchronize threads.