Posts

Map interface and equals()

The Map interface cannot have duplicate keys and implies that equals is used to determine this. boolean containsKey(Object key) Returns true if this map contains a mapping for the specified key. More formally, returns true if and only if this map contains a mapping for a key k such that (key==null ? k==null : key.equals(k)). (There can be at most one such mapping.) The Maps which support this are HashMap LinkedHashMap ConcurrentHashMap Hashtable WeakHashMap The Maps which don't use equals() are IndentityHashMap TreeMap ConcurrentSkipListMap EnumMap All have different reasons as to why they violate the contract but it strikes me as not a great rule that it is as often violated as followed.

Java memes which refuse to die

Also titled; My pet hates in Java coding. There are a number of Java memes which annoy me, partly because they were always a bad idea, but mostly because people still keep picking them up years after there is better alternatives. Using StringBuffer instead of StringBuilder The Javadoc for StringBuffer from 2004 states As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization. Not only is StringBuilder a better choice, the occasions where you could have used a synchronized StringBuffer are so rare, its unlike it was ever a good idea. Say you had the code // run in two threads sb.append(key).append("=").append(value).append(", "); Each append is thread safe, but the lock could be release at any point meaning you c...

Uses for special characters in Java code

Overview Ever wondered how you can write code like this in Java? if( ⁀ ‿ ⁀ == ⁀ ⁔ ⁀ || ¢ + ¢== ₡) Background Underscores has long been using in C like language such as Java to distinguish fields and method names. It is common to see a leading underscore like _field or an underscore in a constant like UPPER_CASE . In Java the $ is also used in class names and accessor method names. The SCJP has notes which state Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number! This leads to the question; what other connecting characters are there? What are connecting characters? A connecting character joins two words together. This page lists ten connecting characters U+005F LOW LINE _ view U+203F UNDERTIE ‿ view U+2040 CHARACTER TIE ⁀ view U+2054 INVERTED UNDERTIE ⁔ view U+FE33 PRESENTATION FORM FOR VER...

Odd practices in Java

Overview There are a number of practices in Java which oddly baffle me. Here are but a few. Using -Xmx and -Xms The option -Xmx is widely used to set the maximum memory size. As noted in the Java HotSpot VM Options  Options that begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK.  So you would think that such a widely used option would not be non-standard any more.  In fact there is a standard option -mx and similarly -ms . I don't know why these standard options are not use more widely, or even documented. Using NIO for non-blocking IO only Non blocking IO was a new feature of NIO for sockets.  However, the default behaviour for NIO Socket is blocking. Files are only blocking in NIO. NIO2 provides an asynchronous interface, but does it by passing your request off to an ExecutorService (which is cheating ...

My first article in Oracle Magazine

Introduction to JIT COmpliation in Java Hotspot JVM Thank you to my co-author Ben, without whom this would never have happened. :)

What is latency, throughput and degree of concurrency?

chrisapotek asked. How do you define throughput and latency for your test? There is not a simple question, so I have replied with a post. Sustained Throughput I consider throughput to be the number of actions a process can perform over a sustained period of time, between 10 seconds and day. (Assuming you have a quite period over night to catch up) I measure this as the number of actions per second or mega-bytes (MB) per second, but I feel the test needs to run for more than a second to be robust. Shorter tests can still report a throughput of X/s but this can be unrealistic because systems are designed to handle bursts of actively with caches and buffers. If you test one behaviour alone you get a figure which assumes nothing else is running on the system and the limits of these buffers are not important. When you run a real application on a real machine doing other things, they will not have full use of the caches, buffers, memory and bandwidth and you may not get within 2-3x the...

yield(), sleep(0), wait(0,1) and parkNanos(1)

Overview On the surface these methods do the same thing in Java; Thread.yield(), Thread.sleep(0), Object.wait(0,1) and LockSupport.parkNanos(1) They all wait a sort period of time, but how much that is varies a surprising amount and between platforms. Timing a short delay The following code times how long it takes to repeatedly call those methods. import java.util.concurrent.locks.LockSupport; public class Pausing { public static void main(String... args) throws InterruptedException { int repeat = 10000; for (int i = 0; i On Windows 7 The average time to yield 0.3 μs, sleep(0) 0.6 μs, wait(0,1) 999.9 μs and LockSupport.parkNanos(1) 1000.0 μs The average time to yield 0.3 μs, sleep(0) 0.6 μs, wait(0,1) 999.5 μs and LockSupport.parkNanos(1) 1000.1 μs The average time to yield 0.2 μs, sleep(0) 0.5 μs, wait(0,1) 1000.0 μs and LockSupport.parkNanos(1) 1000.1 μs On RHEL 5.x The average time to yield 1.1 μs, sleep(0) 1.1 μs, wait(0,1) 2003.8 μs and LockSuppor...