Title: Object Locks
1Object Locks
- When a thread calls a synchronized method, the
object becomes locked - Analogy
- Locking inside a room/booth to perform a task ,
such as a telephone call. - Other Threads/People have to Wait till you
open the door and signal them that the
object/phone is ready to be accessed.
2Scheduling
- Thread Scheduler
- Decides which thread, that is waiting and ready
gets the first access to the object - Thread leaves a synchronized method, it
relinquishes the object lock. - So that other threads can now obtain the lock and
perform the necessary operations
3Number of Locks
- Two different Objects of the same class, can be
locked by different threads - However, an objects lock can be owned by only one
thread at any given point of time. - But a thread can hold locks of multiple objects
at the same time.
4A simple case
- Check Balance
- Transfer Amount
- Synchronized void transfer(bank)
- Check Balance
- TransferAmount
-
- What if there is not enuf money to transfer?
- Since it is synchronized, no other thread can
enter the bank object
5Wait()
- Lets wait till there is enuf money
- Synchronized void transfer(bank)
- Check Balance
- Wait()
- TransferAmount
-
- Use the wait() method of the Object class if you
need to wait inside a synchronized method.
6Wait()
- When wait() is called inside a synchronized
method, current thread is blocked and gives up
object lock. - Wait() is a method of class Object and not of
class Thread. - If a thread holds the locks to multiple objects,
then the call to wait unlocks only the object on
which wait() is called
7Waitlist
- Once a thread calls wait() method, it enters the
wait list of the object - The thread is now blocked
- Until the thread is removed from the waitlist the
scheduler ignores it and does not have a chance
to continue running.
8Notify()
- To remove the thread from waitlist, some other
thread must call notify() or notifyAll(). - notifyAll() removes all threads from the objects
list - Notify() removes only one arbitrarily chosen
thread. - When threads are removed from waiting list, they
are runnable and scheduler can activate them
9Scheduling Rules
- If two or more threads modify an object, declare
the methods that carry out modification as
synchronized. Read only that are affected by
modifications must be synchronized. - If a thread must wait for the state of an object
to change, it should wait inside the object, by
entering a synchronized method and calling wait
10Scheduling Rules
- Dont spend significant amount in synchronized
method. - Whenever a method changes the state of an object
it should call notifyAll(). That gives waiting
threads a chance to see if circumstances have
changed
11Scheduling Rules
- Remember wait() and notifyAll()/notify() are
methods of Object class, not the Thread Class.
Double-check that your calls to wait() are
matched by a notification on the same object.
12Deadlock
- Quite possible to create code that deadlocks
- Thread 1 holds lock on A
- Thread 2 holds lock on B
- Thread 1 is trying to acquire a lock on B
- Thread 2 is trying to acquire a lock on A
- Deadlock!
- Not easy to detect when deadlock hasoccurred
- other than by the fact that nothing is
happening
13Deadlock Wait graphs
14Waitgraph - example
15Deadlock -Causes
- Four preconditions that must be present for a
deadlock to occur - Mutual exclusion Only one process can use a
resource at a time. - Hold and wait A process must hold the resources
while awaiting assignment of other resources. - No preemption No resource can be forcibly
removed from a process holding it. - Circular wait A closed chain of processes
exists, such that each process holds at least one
resource needed by the next process in the chain.
16Livelock
- Deadlock arises when blocked threads cannot
execute - Livelock occurs when threads actually are
executing, but no work gets done. - E.g. one thread keeps performing work that gets
undone by another thread, so it must keep
starting over
17Livelock
- A condition that occurs when two or more
processes continually change their state in
response to changes in the other processes. The
result is that none of the processes will
complete. - i.e. A situation where the computing power is
excessively consumed by interrupt processes.
18Deadlock versus Livelock
- Livelock is similar to deadlock in that no
progress is made. - Livelock differs from deadlock in that processing
continues to take place, rather than just waiting
in an idle loop. - In livelock no process is blocked or waiting for
anything.
19Deadlock versus Livelock
The hallway is wide enough for two people to
pass. If on the same side of the hallway, a
polite strategy is to step to the other side. A
more belligerent strategy is to wait for the
other person to move. Two belligerent people will
suffer in deadlock, glaring face to face in front
of each other. Two polite people could suffer
from livelock if they repeatedly side-step
simultaneously. (No conclusions on morality are
to be inferred from the fact that one polite and
one belligerent person don't have any problems.)