Posts

Showing posts with the label concurrency

Thread Safety Issues with Vector and Hashtable

Considered legacy since Java 1.2 (1998), Vector and Hashtable remain prevalent in many Java applications. A common belief is that their synchronised methods render them inherently thread-safe. However, this assumption can lead to subtle concurrency problems that are often overlooked. Have you encountered unexpected exceptions when iterating over a Vector in a multi-threaded environment? Let’s delve into why this happens and how to address it. The Misconception of Thread Safety Both Vector and Hashtable synchronise individual method calls, leading many developers to assume that these classes are safe to use concurrently without additional synchronisation. While each method is thread-safe, combining multiple method calls can introduce race conditions if not properly managed. Are Iterators Thread-Safe? The Iterator is not thread-safe for most collections, including Vector . Iterators are designed to fail fast by throwing a ConcurrentModificationException when t...

Distributed Unique Time Stamp Identifiers

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

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

Unusual Java: StackTrace Extends Throwable

Image
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); ...