Object
class to achieve ITC:
wait()
:
notify()
or notifyAll()
on the same object.
notify()
:
notifyAll()
:
class MovieEarning extends Thread
{
int total_earning = 0;
int ticket_price = 250; // Price of one movie ticket
int tickets_sold = 10; // Total tickets sold
@Override
public void run()
{
synchronized(this)
{
total_earning = ticket_price * tickets_sold;
// Notify main thread that calculation is done
this.notify();
}
}
}
public class MainApp
{
public static void main(String[] args) throws InterruptedException
{
MovieEarning me = new MovieEarning();
me.start();
synchronized(me)
{
me.wait(); // Wait until MovieEarning thread notifies
System.out.println("Total movie earnings: Rs. " + me.total_earning);
}
}
}
Total movie earnings: Rs. 2500
wait()
, notify()
, or notifyAll()
.
wait()
releases the lock, allowing other threads to acquire it.
notify()
/ notifyAll()
do not release the lock immediately; the awakened thread runs only after the lock is free.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.