Posts

Puzzler: nested computeIfAbsent

Overview The Java 8 libraries have a new method on map, computeIfAbsent.  This is very useful way to turn your Map into a cache of objects associated with a key. However, there is a combination you might not have considered; what happens if you call computeIfAbsent inside itself. map.computeIfAbsent(Key. Hello , s -> { map.computeIfAbsent(Key. Hello , t -> 1 ); return 2 ; }); enum Key { Hello } While this might seem like a strange thing to do in simple cases, in more complex code you could do this by accident (as I did this afternoon)  So what happens?  Well it depends on the collection you are using. HashMap: {Hello=2} WeakHashMap: {Hello=2} TreeMap: {Hello=2} IdentityHashMap: {Hello=2} EnumMap: {Hello=2} Hashtable: {Hello=2, Hello=1} LinkedHashMap: {Hello=1, Hello=2} ConcurrentSkipListMap: {Hello=1} ConcurrentHashMap:  Note: ConcurrentHashMap never returns.  It's locking doesn't appear to be re-entrant. Co...

What are the bad features of Java.

Overview When you first learn to develop you see overly broad statements about different features to be bad, for design, performance, clarity, maintainability, it feels like a hack, or they just don't like it. This might be backed by real world experience where removing the use of the feature improved the code.  Sometimes this is because the developers didn't know how to use the feature correctly, or the feature is inherently error prone (depending whether you like it or not) It is disconcerting when either fashion, or your team changes and this feature becomes fine or even a preferred methodology. In this post, I look at some of the feature people like to hate and why I think that used correctly, they should be a force for good.  Features are not as yes/no, good/bad as many like to believe. Checked Exceptions I am often surprised at the degree that developers don't like to think about error handling.  New developers don't even like to read error messages. I...

Inconsistent operation widen rules in Java

Overview When you perform a unary or binary operation in Java the standard behaviour is to use the widest operand (or a wider one for byte, short and char).  This is simple to understand but can be confusing if you consider what the optimal type is likely to be. Multiplication When you perform multiplication, you often get a much large number than either of the individual numbers in magnitude. i.e. |a*b| >> |a| and |a*b| >> |b| is often the case.  And for small types this works as expected Consider this program public static void main(String[] args) throws IOException {     System.out.println(is(Byte.MAX_VALUE * Byte.MAX_VALUE));     System.out.println(is(Short.MAX_VALUE * Short.MAX_VALUE));     System.out.println(is(Character.MAX_VALUE * Character.MAX_VALUE));     System.out.println(is(Integer.MAX_VALUE * Integer.MAX_VALUE));     System.out.println(is(Long.MAX_VALUE * Long.MAX_VALUE)); } static Str...

Java 8 Optional is not just for replacing a null value

Overview In Java 8, you can return an Optional instead of return null; as you might do in Java 7.  This may or may not make a big difference depending on whether you tend to forget to check for null or whether you use static code analysis to check to nullalbe references. However, there is a more compelling case which is to treat Optional like a Stream with 0 or 1 values. Simple Optional Use Case In the old days of Java 7 you would write something like String text = something(); if (text != null) { Note: Oracle Java 7 will be "End of Public Updates" in April 2015. With Optional you can instead write Optional text = something(); if (text.isPresent()) {     String text2 = text.get(); However, if you are being paranoid you might write. Optional text = something(); if (text != null && text.isPresent()) {     String text2 = text.get(); If you have NullPointerException errors often in your project Optional mi...

micro-timing

With micro-services, micro-fortnights and more micro-buzz words, I was wondering which micro-buzz words might actually be useful if only to confuse people. micro-day - about 1/12 of a second. micro-fortnight - ~1.2 seconds. micro-year - ~1/2 a minute. micro-decade - ~5 minutes. micro-century - 51.3 minutes. micro-mile - ~1.6 milli-metres. micro-light second - ~300 meters. Now you have some more micro-terms you can use to avoid buzz word bingo. ;) Some more to play with atto-parsec - about 3 cm. femto-parsec - about 30 metres. An IT 5 more minutes - maybe half an hour.

Mixing memory unit messages

One of my pet hates is developers messing up units of memory.  In general, the difference doesn't matter much, or you can work out what they meant to say, but it does annoy me when developers who should know better appear to be confused about what they are trying to say. From Wikipedia's Byte  page, here is a cheat sheet of units "b" means bits "B" means bytes "k" or kilo- is standard prefix meaning 1000 "K" or "Ki", sometimes called kibi- is 1024 "m" or mill-, means 1/1000th "M" or mega-, means 1000^2 "Mi" or mebi-, means 1024^2  Unit       Meaning       In bytes mb     milli-bits 1/8000th of a byte mB     milli-bytes 1/1000th of a byte kb     kilo-bits 125 bytes kB     kilo-bytes 1000 bytes Kb or Kib     kibi-bits 128 bytes KB or KiB     kibi-bytes 1024 bytes Mb     mega-bits 125,000 bytes MB     mega-bytes ...

Java Lambdas and Low Latency

Image
Overview The main question around the use of Lambdas in Java and Low Latency is; Does they produce garbage and is there anything you can do about it?  Background I am working on a library which supports different wire protocols.  The idea being that you can describe the data you want to write/read and the wire protocol determines if it uses text with fields like JSon or YAML, text with field numbers like FIX, binary with field names like BSON or a Binary form of YAML, binary with fields name, field numbers or no field meta at all.  The values can be fixed length, variables length and/or self describing data types. The idea being that it can handle a variety of schema changes or if you can determine the schema is the same e.g. over a TCP session, you can skip all that and just send the data. Another big idea is using lambdas to support this. What is the problem with Lambdas The main issue is the need to avoid significant garbage in low latencies appl...