views:

182

answers:

3

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'.

+5  A: 

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.).

Michael Aaron Safyan
To me, 'mutex' is common abbreviation for 'mutual exclusion' not a particular primitive even though it may be the name of a primitive in a particular language.
Edward D'Souza
@Edward, in both UNIX and Windows, mutex is a particular primitive that supports lock, unlock, and trylock. Whereas the Java monitor also supports wait and notify which are aspects of condition variables.
Michael Aaron Safyan
@Safyan, fair enough, but doesn't 'synchronization' refer generally to establishing timing relationships between tasks? Mutex may be an overloaded term but it seems clearer than 'synchronized'.
Edward D'Souza
@Edward, yes, that is true... synchronization is communicating/coordinating the ordering/timing of events. However, I think that makes perfect sense; when you synchronize threads, you impose a serial ordering on them so that they perform an action one after the other instead of executing the action interleaved.
Michael Aaron Safyan
@Safyan, did not mean to say that 'synchronized' was wrong, just unnecessarily ambiguous. Please see edit to question.
Edward D'Souza
A: 

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.

mdma
I think you meant to say, "Using synchronized in itself."
Matt Ball
A: 

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.