i need to write a java program but i need some advice before starting on my own.
The program i will be writing is to do the following:
-
Simulate a shop takes advanced order for donuts
-
The shop would not take further orders, once 5000 donuts have been ordered
Ok i am kind of stuck thinking if i should be writing the java-class to act as a Monitor or should i use Java-Semaphore class instead?
Please advice me. Thanks for the help.
Any java object can work as a monitor via the wait/notify methods inherited from
Object:Just make sure to hold the lock on the monitor object when calling these methods (don’t worry about the
wait, the lock is released automatically to allow other threads to acquire it). This way, you have a convenient mechanism for signalling among threads.It seems to me like you are implementing a bounded producer-consumer queue. In this case:
waiton a shared monitor and go to sleep.notifyon the monitor to wake up the consumer if it’s waiting.notifyon the monitor to wake up the producer.waitand goes to sleep.For an even more simplified approach, have a loop at the various implementation of BlockingQueue, which provides the above features out of the box!