Showing posts from February, 2014

Common gotchas in Java

Overview Java is a minimalist language with deliberately less features than other languages, never the less it has edge cases which strange effects, and even some common cases with surprising effects to trip up the unwary.  If you are used to reading another language you can easily read Java the wrong way leaving to confusion. Variables are only references or primitives That's right, variables are not Objects.  This means when you see the following, s is not an object , it is not a String, it is a reference to a String     String s = "Hello"; This answers many areas of confusion such as;  Q: If String is immutable how can I change it. e g. s += "!"; A: You can't in normal Java, you can only change a reference to a String. == compares references, not their contents. To add to the confusion, using == some times works.  If you have two immutable values which are the same, the JVM can try to make the references the same too. e.g.

Hardware Transactional Memory in Java, or why synchronized will be cool again.

Overview  Hardware Transaction Memory has the potential to allow multiple threads to speculatively access the same data structure at the same time and let the cache coherence protocol determine if a conflict occurred.  HTM aims to give you the scalability of fine grain locking, the simplicity of course grain locking, and performance close to no locking at all.  If supported by the JVM, your program or library was written with course grain lock it could mean your application can scale to many more cores with little change. While adding support for this into C and C++ is non trivial, adding support to native code generated by the JVM might be done without having to change the byte code. In short, this could allow many threads to speculatively execute synchronized blocks for a lock concurrently, even concurrent writes, and the processor work out whether this was a problem and repeats the block until it is not. What is Hardware Transaction Memory and how much will it cost?  Hardwa