News for hackers. Stuff that matters.
Re:Using JDK 9 Memory Order Modes by aardvark179 on Wednesday March 15, 2017 @10:42AM (#13876414)
> This guide is mainly intended for expert programmers familiar with Java concurrency, but unfamiliar with the memory order modes available in JDK 9 provided by VarHandles.

I think it's important to emphasise this. You should think extremely carefully before reaching for features like these and should have a very good reason for not using the existing concurrency tools and data structures. That said, if you do need to build your own concurrent data structures then these do provide excellent primitives with which to do it.

Re:Using JDK 9 Memory Order Modes by derefr on Wednesday March 15, 2017 @01:33PM (#13878008)
Right; the proper consumer of material like this (for anything other than intellectual-curiosity purposes) isn't your regular line-of-business developer; it's more the the developers of the concurrency libraries such developers consume, e.g. actor frameworks like Akka or Vert.x.
Re:Using JDK 9 Memory Order Modes by aardvark179 on Wednesday March 15, 2017 @02:14PM (#13878399)
I was going to say it was even more limited than that, but vert.x does seem to have quite a few uses of AtomicLong, Integer, and Boolean which could be replaced by primitive fields and VarHandles. Whether it's worth it depends on how many of them are actually used at run time and whether removing the extra indirection and footprint is important.

It also makes heavy use of the JDK's concurrent maps, latches, and other structures which it would probably be unwise to try and re-implement.

Re:Using JDK 9 Memory Order Modes by axiak on Wednesday March 15, 2017 @03:59PM (#13879268)
Those uses of AtomicFoo classes could probably be updated to use AtomicFooUpdater fields (e.g. AtomicIntegerFieldUpdater). VarHandle is like AtomicFieldUpdater but with all of the concurrency semantics that were accessible only via Unsafe access.
Re:Using JDK 9 Memory Order Modes by _old_dude_ on Wednesday March 15, 2017 @05:59PM (#13880230)
In Java 9, all updaters have been rewritten in term of VarHandle. All reference updaters are less efficient that their VarHandle counterparts because they rely on generics.

In Java generics are erased at runtime, so the reference updater code has some supplementary runtime checks.

Re:Using JDK 9 Memory Order Modes by sreque on Wednesday March 15, 2017 @08:30PM (#13881280)
This isn't correct. The atomic updater classes don't rely on generics to do anything. What they do is perform safety checks on every access to ensure you don't segfault your JVM, resulting in heavy performance losses compared to sun.misc.Unsafe.
Re:Using JDK 9 Memory Order Modes by xxs on Thursday March 16, 2017 @03:07AM (#13883115)
Actually it is true (about generics and erasure) when it comes to AtomicReferenceFieldUpdater. The check is likely to be around 1 cpu cycle since the branch is virtually always predicted.
Re:Using JDK 9 Memory Order Modes by _old_dude_ on Thursday March 16, 2017 @05:31AM (#13883670)
It can be one mov + cmp + je in assembly, but it's not always the case.

It's an instanceof check, if the class of the field is final (or can be proven to be effectively final by CHA) then it's a null check follow by a pointer check. Theoretically, the null check can proved to be unnecessary, be fused with another or be transformed to an implicitly nullcheck but in specific the case of an AtomicReferenceFieldUpdater, null is usually a valid value (by example, null is used as tombstone in many concurrent data structure implementations) so it's usually more than just a getClass() and a pointer check.

Re:Using JDK 9 Memory Order Modes by sreque on Thursday March 16, 2017 @06:38PM (#13889813)
From what I've read, until recently this wasn't actually the case for the field updater classes. Instead, Java does things like not trusting your fields' finality and not treating anything as constant. Let me see if I can find the blog post.

Here we go: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&e...

Re:Using JDK 9 Memory Order Modes by sreque on Thursday March 16, 2017 @06:36PM (#13889797)
In this sense VarHandle also relies on generics when updating references, which is completely meaningless, just as it is meaningless to say AtomicReferenceFieldUpdater "relies on generics".