Nine Core Java Questions

My previous post with Eight Core Java Questions was popular so I created nine more. Please join the discussion of the answers for these questions on Twitter 1. Comment on this question How many elements does this set have? Set set = new HashSet(Arrays.asList( -0, +0, -0L, +0L, -0F, 0F, -0D, 0D, 0xF, 0xD, 0x0.DP+0)); System.out.println(set.size()); 12 1 6 9 2. Comment on this question This compiles in Java 17 due to static int[] a(int[]... a)[] { return a; } static final int one = a(new int[0]).length; // 1 It confuses the compiler It’s a static method Backward Compatibility JEP 747 3. Comment on this question The code below prints "23 & 52" the following due to System.out.println( new BigDecimal(2.008f).toString().length() + " & " + new BigDecimal(2.008).toString().length()); A bug in Java 23 The randomness of floating-point Length of the mantissa Magic numbers

Eight Core Java Questions

Please join the discussion of the answers for these questions on Twitter The answers to each question can be combined for a bonus question at the end. 1. Which of these is NOT a means of communicating between processes in Core Java. Comment on Twitter UDP Pipes/Files Pass by reference TCP 2. Which of these methods of copying an object is the LEAST efficient? Comment on Twitter Java Serialization clone() Copying field by field Using a builder 3. Which of these is the lowest amount of time Comment on Twitter A micro-day A nano-year 0.001 seconds A millsecond A million nanoseconds 100 microseconds 4. This loop doesn’t throw an Exception Comment on Twitter List<String> words = new ArrayList<>(Arrays.asList("one,two,three".split(","))); for (String word : words)   if (word.startsWith("t"))     words.remove(word); Because It’s in proportional font A thread s

Benchmarking Kafka vs Chronicle for Microservices: which is 750 times faster?

Apache Kafka is a common choice for inter-service communication. Kafka facilitates the parallel processing of messages and is a good choice for log aggregation. Kafka claims to be low latency, high throughput . However, is Kafka fast enough for many microservices applications in the cloud? When I wrote Chronicle Queue Open Source my aim was to develop a messaging framework with microsecond latencies, and banks around the world have adopted it for use in their latency-sensitive trading systems. In this article, I will describe how Kafka does not scale in terms of throughput as easily as Chronicle Queue for microservices applications. As a teaser, I will show you this chart showing that Chronicle Queue is around 750 times faster even for lower throughput. Visualising delay as a distance In order to illustrate the difference, let me start with an analogy. Light travels through optic fibre and copper at about two thirds the speed of light in a vacuum, so to appreciate very short de

Distributed Unique Time Stamp Identifiers

Recently I published an article on using timestamps as unique identifiers , generated in a fraction of a microsecond. This article covers an implementation that supports distributed identifier generation directly. This specific implementation supports up to one billion new 64-bit identifiers every second only repeating after 520 years. They can also be printed as timestamps containing the wall clock to make it easier to read. Concurrent identifier generation in a distributed system Each host has a predefined, unique host identifier, or hostId . This TimeProvider assumes up to 100 hosts to produce different identifiers concurrently.  JVMs using the same hostId must be on the same physical machine using the same memory-mapped file, or you can give each JVM a different hostId A nano-second timestamp with a host identifier DistributedUniqueTimeProvider stores a host identifier in the lower two digits of the timestamp making it easier to read. The previous implementation used bit shifting

System wide unique nanosecond timestamps

A Unique Identifier can be very useful for tracing. Those ids are even more useful when they contain a high-resolution timestamp.  Not only do they record the time of an event, but if unique can help trace events as they pass through the system. Such unique timestamps however can be expensive depending on how they are implemented.   This post explores a lightweight means of producing a unique, monotonically increasing system-wide nano-second resolution timestamp available in our open-source library. Uses for Unique Identifiers Unique identifiers can be useful to associate with a piece of information so that information can be referred to later unambiguously. This could be an event, a request, an order id, or a customer id. They can naturally be used as a primary key in a database or key/value store to retrieve that information later. One of the challenges of generating these identifiers is avoiding creating duplicates while not having an increasing cost.  You could record every identi

Unusual Java: StackTrace Extends Throwable

There are things you can do in Java you rarely see, generally because there is no use for it. However, there are some unusual things in Java that could be surprisingly useful. Chronicle Software uses a number of different usual patterns in it’s low-level libraries most developers wouldn’t generally come across. One of them is a class that extends Throwable but isn’t an Error or an Exception. StackTrace Extends Throwable package net.openhft.chronicle.core; /** * Throwable created purely for the purposes of reporting a stack trace. * This is not an Error or an Exception and is not expected to be thrown or caught. */ public class StackTrace extends Throwable {     public StackTrace() { this ( "stack trace" ); }     public StackTrace(String message) { this (message, null ); }     public StackTrace(String message, Throwable cause) {         super (message + " on " + Thread. currentThread ().getName(), cause);    }     public static StackTrace forThread(Thread t) {