Skip to main content


Showing posts from 2018

A brief overview of the Fork/Join Framework in Java

Introduction The Fork/Join framework is a framework to solve a problem using a concurrent divide-and-conquer approach. They were introduced to complement the existing concurrency API. Before their introduction, the existing ExecutorService implementations were the popular choice to run asynchronous tasks, but they work best when the tasks are homogenous and independent. Running dependent tasks and combining their results using those implementations were not easy. With the introduction of the Fork/Join framework, an attempt was made to address this shortcoming. In this post, we will take a brief look at the API and solve a couple of simple problems to understand how they work. Solving a non-blocking task Let's jump directly into code. Let's create a task which would return the sum of all elements of a List. The following steps represent our algorithm in pseudo-code: Find the middle index of the list Divide the list in the middle Recursively create a new task whic

Java Tips: Creating a Monitoring-friendly ExecutorService

In this article we will be extending an ExecutorService implementation with monitoring capabilities. This monitoring capability will help us to measure a number of pool parameters i.e., active threads, work queue size etc. in a live production environment. It will also enable us to measure task execution time, successful tasks count, and failed tasks count. Monitoring Library As for the monitoring library we will be using Metrics . For the sake of simplicity we will be using a ConsoleReporter which will report our metrics to the console. For production-grade applications, we should use an advanced reporter (i.e., Graphite reporter). If you are unfamiliar with Metrics, then I recommend you to go through the getting started guide . Let's get started. Extending the ThreadPoolExecutor We will be using  ThreadPoolExecutor as the base class for our new type. Let's call it  MonitoredThreadPoolExecutor . This class will accept a MetricRegistry as one of its construct

JPA Tips: Avoiding the N + 1 select problem

Introduction ORM frameworks like JPA simplifies our development process by helping us to avoid lots of boilerplate code during the object <-> relational data mapping. However, they also bring some additional problems to the table, and N + 1 is one of them. In this article we will take a short look at the problem along with some ways to avoid them. The Problem As an example I will use a simplified version of an online book ordering application. In such application I might create an entity like below to represent a Purchase Order - @Entity public class PurchaseOrder { @Id private String id; private String customerId; @OneToMany(cascade = ALL, fetch = EAGER) @JoinColumn(name = "purchase_order_id") private List<PurchaseOrderItem> purchaseOrderItems = new ArrayList<>(); } A purchase order consists of an order id, a customer id, and one or more items that are being bought. The PurchaseOrderItem entity might have the fo