Campbell Ritchie wrote:Question too difficult for this forum: moving to our Threads forum.
And welcome to the Ranch
Here it is.
stathis papapa wrote:. . . where exactly is the "Threads" forum??
Stephan van Hulst wrote:Also, please do yourself a favor and don't call the semaphores p and v.
Instead, name them insidePermitForA and insidePermitForB. After you've written the correct code, you will see that this reads very naturally and makes the code clear to anyone trying to read it.
Stephan van Hulst wrote:That's definitely not Java.
Have you tried compiling your code yet?
Tim Holloway wrote:May I present my good friend, the Wikipedia?
Actually, this isn't the simplest explanation I've seen, but it's relatively short.
One thing that may be confusing you is that in Java, we have the concept of "synchronized" code. Java manages synchronized code with semaphores, but those semaphores are hidden inside the Java synchronization mechanism, so that you don't really see the "P" and "V" methods at work - or even the actual semaphore.
Java 5 did add an explicit Semaphore class, but the original synchronization features are sufficient for most people.
There's also something called a "spin lock" (and locks in general), but they are used for situations where the overhead of placing a thread on a waiting queue and pulling it off takes too much time. You use a spin lock when you need the locked resource within a few microseconds at most, and you're willing to simply spin around in a loop testing until you can get it instead of doing a thread-wait. That's mostly used by low-level OS code.
Stephan van Hulst wrote:In your own words, tell us what a Mutex is and what operations you can perform on it and what those operations do. Do the same thing for Semaphore.
Campbell Ritchie wrote:I don't know whether this Wikipedia article is any good, but many of them are good.
Stephan van Hulst wrote:It's important to know these primitives . . . combine the use of a mutex and semaphores into a complete program.
Stephan van Hulst wrote:We'd like to help you, but you have to be specific in what you don't understand. What is so alien to you?
Stephan van Hulst wrote:What don't you understand about the result?