This is a short article to give you a taste of the Java Memory Model. Being unaware of its rules can lead to some pretty knotty bugs. Any serious Java developer who wishes to write thread-safe parallel programs should be aware of the memory visibility issues that arise due to the JMM.

What Is a Memory Model?

Any modern computer architecture can do such things like cache global variables and reorder instructions to get a performance boost. It is not guaranteed that the instructions are run in the order as they were written in the high-level program. In a single…


.flatMap() is one of the most arcane of Reactive’s operators. While it is difficult to understand, once we understand exactly how it works, it becomes an extremely powerful operator in our toolkit. In this article, we’ll try to dissect it and understand what it does and why it does what it does. Note: I am using Project Reactor for demonstration but RxJava’s .flatMap() also works in the same way. …


flatMap’s Marble Diagram

In the previous post, we looked at the basics of how flatMap works. This post can be considered an addendum to that post.

flatMap has two other overloaded versions:

  1. flatMap(Function<? super T, ? extends Publisher<? extends V>> mapper, int concurrency)
  2. flatMap(Function<? super T, ? extends Publisher<? extends V>> mapper, int concurrency, int prefetch)

In this post, I’ll be covering the first variant (the 2nd one is easy to understand once you understand the first variant). Concurrency is the number of inner streams flatMap subscribes to simultaneously. This is also the number of events that flatMap requests from the upstream, and…


I have been trying to understand David’s Goldberg’s seminal article What Every Computer Scientist Should Know About Floating-Point Arithmetic for some time now. As a programmer with no formal background in numerical analysis, floating-point was always (and still is) intimidating to me. And rightly so — floating-point is difficult and arcane, and if a programmer wants to write correct FP programs, he needs to understand how it works. In this article, I have tried to cover some of the basics of floating-point.

First, why floating-point? As we all know, the computer is a finite-state machine. It was assume only so…


Imagine a class like this:

public class Engine {
private Gasoline gas = new Gasoline();

public Double getRemainingGas() {
return this.gas.getRemaining();
}
}

We basically have a class called Engine. An instance of this class is dependent on an instance of the class Gasoline (tight coupling, bad). Now suppose we want to write a unit test for this class. Remember, unit tests are to test the class in isolation — we try to mock its dependencies and use them to write assertions.

Note that it is not possible to write a unit test for this class. It is because when…


I have been working with Kafka for the past few months. Recently, I programmed a critical part of our system — a Kafka poller that polls the Fulfilment Service (FS). Writing a robust poller while ensuring that records are processed as per the business needs (and parallelising wherever possible) and testing such an asynchronous system is I would say, a bit complex and requires a thorough understanding of how Kafka works (at least its API, if not its architecture). Through this post, I have tried to summarise what I learnt, what worked for me, and what didn’t. …

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store