Contents
Can a thread acquire more than one lock?
Ans) Yes. A thread can hold multiple locks at the same time. Once a thread acquires a lock and enters into the synchronized method / block, it may call another synchronized method and acquire a lock on another object.
Can threads of different blocks be synchronized?
A synchronized block in Java is synchronized on some object. All synchronized blocks synchronized on the same object can only have one thread executing inside them at a time. All other threads attempting to enter the synchronized block are blocked until the thread inside the synchronized block exits the block.
Can a thread enter a synchronized block without acquiring a lock?
synchronized keyword involve locking and unlocking. It means that if a java synchronized method calls another synchronized method which requires the same lock then the current thread which is holding lock can enter into that method without acquiring the lock.
How do you release a lock in synchronized block?
Thread inside the synchronized method is set as the owner of the lock and is in RUNNABLE state. Any thread that attempts to enter the locked method becomes BLOCKED. When thread calls wait it releases the current object lock (it keeps all locks from other objects) and than goes to WAITING state.
What is the difference between a synchronized method and a synchronized block?
synchronized method acquires a lock on the whole object. This means no other thread can use any synchronized method in the whole object while the method is being run by one thread. synchronized blocks acquires a lock in the object between parentheses after the synchronized keyword.
What is the difference between synchronized block and method?
The difference is in which lock is being acquired: synchronized method acquires a lock on the whole object. This means no other thread can use any synchronized method in the whole object while the method is being run by one thread.
Why do we need to use locks in synchronization?
Because the use of locks requires threads to wait ( acquire blocks when another thread is holding the lock), it’s possible to get into a a situation where two threads are waiting for each other — and hence neither can make progress. In the figure to the right, suppose A and B are making simultaneous transfers between two accounts in our bank.
What does it mean when two threads synchronize on a method?
If you synchonize on the method you lock the whole object, so two thread accessing a different variable from this same object would block each other anyway. That’s a bit misleading. Synchronizing on the method is functionally equivalent to having a synchronized (this) block around the body of the method.
When does a thread use the same lock more than once?
Allowing a thread to acquire the same lock more than once enables reentrant synchronization. This describes a situation where synchronized code, directly or indirectly, invokes a method that also contains synchronized code, and both sets of code use the same lock.
How are A and B locked up in synchronization?
Now, each must acquire the lock on their “to” account: so A is waiting for B to release the account 2 lock, and B is waiting for A to release the account 1 lock. Stalemate! A and B are frozen in a “deadly embrace,” and accounts are locked up.