From what I understand, yield() on a thread does not release the lock for that thread. So if there are two Threads and both of them have synchronized block in the thread pool, A with lower priority and B with higher priority. If Thread A is running and yield() is called in A, thread B now will run by scheduler. Because currently, thread A is still holding the lock, so thread B can not go into the critical block? From my understanding, this will cause a deadlock?
Please let me know do I miss something?
Thanks very much:)
There is some confusion here.
There is no deadlock, because there is only one lock. You need at least two locks for a deadlock.
yield() does essentially nothing. See the Javadoc.
At any time, possibly including when you call
yield(), the higher-priority thread will block trying to obtain the lock. So it is no longer eligible for execution. So the scheduler will have to schedule something else that is runnable.
Meanwhile the lower-priority thread remains runnable (i.e. eligible for execution), so it will get scheduled sooner or later, so it continues to execute. Sooner or later it will release its lock, which will allow the higher-priority thread to acquire it and continue its execution.