When two threads try to acquire the lock of the same object what are the things that are considered to decide upon to which thread the lock should be handed over.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
According to the Java documentation for notify():
So if you use
synchronized(obj){}you basically have no control on which thread will obtain the lock onobj, and you cannot make any assumption. It depends on the scheduler.If you want fairness (that is, the next thread obtaining the lock is the first in the queue), have a look at ReentrantLock: it has a boolean flag to specify you want to enforce fairness.