Why is the synchronized keyword in Java called 'synchronized' instead of the more precise 'mutexed'?
I've heard that choosing to use the word 'synchronized' to describe mutexed statements is simply a mistake (Edit: 'mistake' was a bad choice of words here. Please see edit) in Java, but I'm wondering开发者_如何学运维 if there is actually a reason behind the choice.
[Edit]
Prodded by Safyan's comments, I would like to add that synchronization is a general term for establishing timing relationships between threads. It can include mutual exclusion and things like rate control (eg. two threads doing something at the same rate). It appears unnecessarily ambiguous to use 'synchronized' to mean mutual exclusion instead of a more specific keyword like 'mutexed'.
It is not a mistake. It means what it says; the code has to synchronize with other threads to provide mutual exclusion. And, in fact, the term synchronized may make more sense than "mutex", since "mutex" implies a very particular type of synchronization primitive, and the synchronized keyword could be implemented using any number of thread syncrhonization primitives (test&set with active polling, semaphores, etc.).
The use of synchronized keyword instead of mutex is actually a good way of expressing the term. By mutex we are not pretty much clear, but synchronized keyword itself tells about itself. Synchronized is placed at the code which actually needs to be synchronized between no of threads which all wants to access that code. That's why it is termed as synchronized.
The synchronized keyword is used to acquire and release the lock on a Monitor. Like Mutexes, Monitors are used for concurrency control, but they are not the same.
Using synchronized in itself is not a mistake, but it can be a low-level construct for using with multi-threading, and inappropriate use can quickly lead to multi-threading errors.
精彩评论