Alex Rivera | Logout

Any reason NOT to slap the 'synchronized' keyword everywhere?

Asked 2009-06-11T03:01:07.520
12

In my java project, almost every non-static method I've written is synchronized. I've decided to fix up some code today, by removing most of the synchronized keywords. Right there I created several threading issues that took quite a while to fix, with no increase in performance. In the end I reverted everything.

I don't see anyone else writing code with "synchronized" everywhere. So is there any reason I shouldn't have "synchronized" everywhere?

What if I don't care too much about performance (ie. the method isn't called more than once every few seconds)?

Edit
Report

1 Answer

2

You shouldn't, and you probably need to reconsider your design. From my experience, synchronization at this level is just not scalable. It might fix your immediate problems, but won't help to fix your overall application integrity.

For example: Even if you do have a "synchronized" collection, one thread may want to call two "synchronized" methods in one go, but don't want another thread to see the intermediate result. So any caller of a fine grained "synchronized" API must be itself aware of that, which heavily degrades the (programmer-) usability of this API.

For a start, it is best to use worker threads. Isolate specific long running tasks so that separate threads take one block of immutable input and - when finished - enqueue the result in the main thread's message queue (this of course should be synchronized). The main thread may poll or explicily wait for a signal if it has nothing else to do. The specific details depend on your performance requirements and application design.

On the long run, try to understand implementations of the ACID transaction model of database implementations or message passing concurrency, ... for example, the Erlang language will give you a lot of inspiration. These are synchronization models that are proven to be scalable. Take the best of them and try to apply these concepts to the multithreaded application you work at.

answered 2009-06-11T03:46:16.453

Your Answer