Java Threads

OK... I've decided that I like pthreads in C++ a lot more than the threading model in Java - at least if you're not totally familiar with the Java model. One of the classes that I've been dealing with this morning is a FIFO Queue. It's thread-safe because those methods that need to be synchronized to ensure single instance access to the data are, and those that might require multiple thread access are left unsynchronized. This seemed reasonable because in the methods, where necessary, I had a synchronized block on the variable of import. I was so very wrong.

The problem is that I had created two mutexes and therein lay my problem. The confusion I had was that a synchronized method with a synchronized block in it containig a wait(); would block all further access to that instance by other threads. The effect of having a synchronized method is really no more than encasing the entire method contents in a synchronized block on this. So... if the wait() was on this then I could simply synchronize the method and the wait() would release the lock and let other threads message this instance.

This made the code a lot easier as the push() method then simply did a notifyAll() when placing something of interest on the queue. Again, if the push() method was synchronized, then the control would pass to the waiting threads and then back to the push() thread and things would just work out.

So really, it's when I didn't understand the actual mutexes in place in the Java code and tried to work up something to do what was necessary that I got into trouble. Now I just lock on the queue instance itself and the synchronized methods do all the hard work for me.