Posts

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

sun.misc.Unsafe and off heap memory

Overview The class sun.misc.Unsafe allows you to many of the things you shouldn't be able to do in Java, but are still useful in very specific cases.  It is to be avoided 99% of the time, however there are rare occasions where this is the only solution which makes sense. This post considers how it has been using in OpenHFT and what functionality I would like to see in Java 9. In particular, accessing large amount of memory without impacting the GC can be done this way. Sharing memory between processes, without significant overhead, can only be done this way in Java. Allocating and freeing off heap memory. public native long allocateMemory(); public  native  void freeMemory(long address); These two method allow you to allocate any size of off heap memory.  It is not limited to Integer.MAX_VALUE bytes and you get raw memory where you can apply bounds checking as you need to. e.g. Bytes.writeUTF(String) calculates the length of the encoded string, and checks t...

Hiring a Java Analyst Developer

If you have enjoyed this blog and looked at the OpenHFT projects such as Java Chronicle you might be interested in applying for this role.  You would learn a lot working on technically challenging projects. Higher Frequency Trading is a consulting and software developer company offering training, performance consulting, system development and open source software to clients in the USA and Europe. We are looking to expand the development and support for existing and new open source projects. This is a great opportunity to get into High Frequency Trading as you will be developing and supporting open source software which is used in HFT systems for investment banks, hedge funds and trading houses.  The project area is https://github.com/openhft Requirements - 5 to 10+ years commercial programming, preferably with some support experience - you must be passionate about low level coding in Java - you must have worked on open source projects  (Please provide...

Interview: Starting out as an IT Consultant.

Peter Pilgrim interviewed Peter Lawrey to find out how he started out as an IT Consultant. [Pilgrim] Starting a business for me is pretty scary and crazy s%#t, no offence. I just do not know how to market myself to the key decision makers for one thing. I am unsure if one week going into a site will work for the typical Java EE project that I see. Some clients take at least that much time to set up access to SVN and produce a good enough laptop that works. I assume that this is different in your experience with low latency trading. [Pilgrim] So Peter, how did you get started? [Lawrey] I preferred the technically challenging and interesting roles but I was finding the same issues coming up and spending less time solving them.  After some short roles, I saw a trend of moving to shorter consulting roles.  I have enjoyed consulting and don't feel under challenged. I am friends with a couple of guys who have a small start up and have been operating for a few y...

Why we shouldn't use more threads than we need to

Overview There is a common argument that because we have lots of cores, and will have even more in the future we have to use them.  We just we need to find the best ways to use them but just because we can doesn't mean we should. What is our goal? Good reasons to use multiple threads are the performance of using one thread is not enough. you have profiled your application to ensure there is no low hanging fruit. multiple threads improve the throughput,  latency or consistency. At this point you should add a thread when you know it gets you closer to your goal. A bad reason to use multiple threads Just because we can use more threads doesn't mean we should.  Multiple threads Adds complexity to the code There are other was to speed up an application.  You L1 cache is 10-20x faster than you L3 cache and if you can spend more time in you L1 cache by optimising your memory usage and access, you can gain more performance than using every CPU in your s...

Computing units don't have to be confusing

Overview Often programmers will use units in a non standard way and usually you can work out what they meant by context.  Many developers never realise there is inconsistencies is technical material as the difference is small to the amount of error and the difficult of reproducing benchmarks reported. There are times when the difference is so large it does matter and the writer has confused themselves by a lack of understanding which makes your job of trying to work out what they done harder. An array of measures b  = bit. B = byte or 8 bits or 8b g = gram kb = kilobit or 1000 bits. kilo being the scientific prefix for 1000 like kilometer. kB = kilobyte or 1000 bytes, sometimes used for storage kg = kilogram Kb = Kibit =  kibib it or 1024 bytes. KB = KiB =  kibibyte  or 1024 bytes, sometimes used for memory Mb = megabit  = 1000^2 bits or 125 kB. Mb/s is used for network bandwidth. MB = megabyte = 1000^2 bytes, MB/s is u...