In October or November of 2018, I was asked a question in interview process by an interviewer from Sapient, Noida, India.
The interviewer Subrata Kundu asked .. What is Thread priority?
I said, thread priority is a kind of relative importance given to threads with respect to each other.
That relative importance can be a int value, or that can be any value depends on the system or the user.
I also added, that we should not design our software around thread priority.
A rude response came from his side, ‘agar use nahi karna hai to diya hua kyun hai?‘
The english translation is, ‘If we should not use it, then why it is provided?‘
So, I am going to provide a brief on why we should not use it.
Suppose there are Two threads T1, T2 and T3,
They all have been executing for sometime and they are doing their work.
And, all the threads have different tasks and they do not need to co-ordinate with each other on anything.
So, basically they are independent of each other and for them existence of one thread over the other does not matter.
But, their existence matters to the operating system. The system/software which manages all the threads.
So, let us assume that T3 has the highest priority over all other threads.
And, T3 was executing, and it was waiting for an input from the user.
So, in the meantime Operating system removes the thread T3 from the processor and, the processor was given to T2.
T2 is executing right now.
And, T2 has taken an exclusive lock on resource A, and this resource is also needed by T3.
Now, T3 can execute as the IO operation is over, so, as per thread priority, T3 should be allowed to execute and T2 should be swapped out of the memory.
But, there is an issue over-here, If T2 is swapped out of the memory, it will take away resource A with it. Because, it has an exclusive lock on this resource.
And, T3 will have to wait for resource A to get released so, that it can proceed.
But, resource A can’t be released because T2 is not allowed to run.
This is a deadlock scenario, now, T3 is waiting for release A but that can not happen, because T2 has the lock and it is swapped out or we can say it is in WAITING/SUSPENDED state.
These are the scenario, where following thread priority will lead to a deadlock.
Because of that, the system is free to ignore the thread priority.
And, OS will not remove lower-priority thread to run a higher priority thread.