3 ms·
Au contraire, the fact that Java actually has a memory model means one can reason about concurrency in a way impossible on other platforms. As you note you rar
by noelwelsh 12y ago
Au contraire, the fact that Java actually has a memory model means one can reason about concurrency in a way impossible on other platforms.
As you note you rarely need to concern yourself with the details of the memory model as most of us are users, not designers, of concurrency primitives. But you should you need to design a new primitive you can actually have some guarantees it will work in a cross-platform manner, which is more than you an say about any other platform I know of.
- zmmmmm 12y agoVery true! Having it defined is very important. However part of the problem is that definition actually takes away guarantees that many CPUs (eg. x86) give you. For example double checked locking works in x86 but famously not in Java. So the fact is that Java is giving you in some ways a less sequential model than what you started with is what makes it particularly insidious.
- pron 12y agoYou are free to rely on the guarantees made by a particular hardware architecture. The JVM implementation doesn't "take them away" -- the spec merely doesn't guarantee them. But if you do, then you might lose portability.
- zmmmmm 12y agoMy impression is that by not guaranteeing them it does take them away, because the implementation is free to implement them in any way they choose including in the compiler. So it will not matter if your code is executing on a processor that does not reorder instructions, Javac may already have done it to the byte code.
- pron 12y agoEven a C compiler is allowed to reorder instructions. How is any compiler supposed to know if you prefer performance (and therefore reordering) or to to rely on a processor-specific feature? That's why any language that runs on more than one architecture needs to define a language-level memory model.
- nradov 12y agoDouble checked locking works correctly in Java since version 1.5.