This is one of the codes I got from my textbook. Can someone help me to understand why is the sem.acquire() and sem.release() necessary when we can keep track of which item is used or unused simply using the getAvailableItem() and markAsUnused()?
In general the semaphore could allow you to constrain concurrently a list of resources to a (different) given number concurrently in use where, also getAvailableItem in intent
would potentially give you a no none available (eg null) whereas the semaphore would wait on the concurrent users to release i.e. its a blocking call.
So getAvailableItem is a kind of try to get one of a big set of resources but immediately fail if not available but getItem is a try to get one if two many users are working wait till one finishes.
I'd suggest getting the code to compile and run it first.
"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5