There is one problem in immediate evidence: the iterator is retrieved outside the synchronized block, so there is an opportunity for another
thread to modify messageTable between calling iterator() and entering the synchronized block.
Another probable problem is that the synchronization will only achieve anything if all the code that might modify messageTable is synchronized as well. And, of course, if processMessage() modifies messageTable you'll also get a ConcurrentModificationException.
The explicit synchronized block is a bit of a "code smell". What I would strongly suggest is not synchronizing on the Map itself, but rather wrapping that map inside your own class that exposes methods that do whatever you want to do. Synchronize the methods on your class. All of them, unless you know what you're doing.
Other things --the if (messageTable == null) will never execute, you'd get a NullPointerException from the previous statement. I would also strongly encourage you not to declare msgiterator or spmsg at the top; their scope becomes much too large which reduces the readability of your code. There is no reason why you wouldn't say
There are
no performance penalties associated with this. There's more strange stuff -- the endless do...while loop -- but I guess you know best what your code is supposed to do
- Peter