Unit 5 Notes
Unit 5 Notes
Unit 5 Notes
Syntax:
Output:
Final count: 2000
Explanation:
• The Counter class has a synchronized increment method.
• Two threads are created and both execute
the increment method 1000 times each.
• The final count is 2000, demonstrating that the synchronization
prevents race conditions.
Output:
Final count: 2000
Explanation:
• The Counter class has an increment method with a
synchronized block.
• Two threads are created and both execute
the increment method 1000 times each.
• The final count is 2000, demonstrating that the synchronization
prevents race conditions.
wait method
The wait() method pauses the current thread execution and waits
until the time elapses or another thread invokes the notify() or
notifyAll() method.
We can invoke the wait() method only within the synchronized
method or synchronized block since the thread must own the
monitor, else it will throw an exception. Once it calls the wait()
method, it releases the current lock monitor.
public final void wait(long milliseconds) throws InterruptedException
What is a monitor
Whenever a synchronized thread wants to access a shared resource,
it acquires a lock and enters the monitor. At a time, only one thread
can own a monitor. The other threads need to wait until the first
thread releases the monitor.
notify method
The notify method awakes the thread that called the wait method on
the same resource. This causes the thread that was sleeping to
resume its execution. If there were multiple threads in the wait state
for the same shared resource, then it notifies any one of them.
public final void notify()
notifyAll method
The notifyAll method awakes all the threads that call the wait
method on the same resource. If there are multiple threads, then it
wakes up the highest priority thread.
public final void notifyAll()
OUTPUT:
Purchase stock
Insufficient quantity available
Adding Product stock
Add stock completed
Purchase stock completed
When the first thread calls the purchaseStock() method, it acquires
the lock and checks if the required quantity is available in the stock.
Since the requested quantity(which is 20) is lesser than the available
quantity( which is 15), it calls the wait() method.
Now it releases the lock and starts the second thread since the first
thread is in the wait state. The second thread calls the addStock()
method which adds the required quantity to the available stock and
then invokes the notify() method. The notify() method wakes up the
first thread that had called the wait() method and resumes the
execution.
Now, since the required stock is available, it decreases the requested
quantity and updates the available stock, and completes the
execution.
Difference between wait and sleep
Though wait and sleep methods perform more or less the same
operation, there are some differences as below.
wait sleep
Belongs to the Object class Belongs to the Thread class
It is a non-static method It is a static method
Wait operation is interrupted by notify or Sleep operation is interrupted after a
notifyAll methods specified timeout
It releases lock when wait method is called It does not release or own any lock