Different States of Java Threads

Introduction In Java, threads can have States. The Thread.State enum defines the different states that a Java thread can have. This enum defines the following values - NEW RUNNABLE BLOCKED WAITING TIMED_WAITING TERMINATED In the subsequent sections I will provide a brief overview of these states along with possible transitions between them. States of a Java Thread NEW This is the default state a thread gets when it is first created....

January 14, 2019

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

December 16, 2018

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

May 1, 2018

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

April 22, 2018

Clean Code From the Trenches Validation

Let’s directly start with an example. Consider a simple web service which allows clients to place order to a shop. A very simplified version of the order controller could look something like below - @RestController @RequestMapping(value = "/", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE) public class OrderController { private final OrderService orderService; public OrderController(OrderService orderService) { this.orderService = orderService; } @PostMapping public void doSomething(@Valid @RequestBody OrderDTO order) { orderService.createOrder(order); } } And the corresponding DTO class -...

May 12, 2017