Monitor and Semaphore
Monitor is an object which is accessed by more than one threads. The methods of a monitor object can be accessed by only one thread at a time. Only one thread will be performing any action on the object. If one thread is currently using a method of the object then other rest of threads that tries to call a method of that object will have to wait until the first thread has finished its usage of that method.
Semaphore is a similar to monitor but you can say it is a lower level object. It works based on counter. You can use a semaphore to implement a monitor. Actually a semaphore just a
counter. when the counter is positive, and if a thread tries to acquire the
semaphore then it is allowed to access it and then the counter is decremented. When a
thread finish usage of semaphore then it releases the semaphore, and it increments the
counter.
It is like you have 10 houses and you gives then on rent when someone ask.
When one house is given on rent you decrements the total available and when that house is left by the rented person you increment the total available house.
If the semaphore counter is zero when a thread want to to acquire the
semaphore then that thread has to wait until another thread releases the
a semaphore and increment the semaphore counter.
If there are multiple threads are waiting for the semaphore then any thread will get a chance.