Posts

Showing posts from May, 2025

A Brief Guide to Several Ways of Implementing Asynchronous Programming in Java

Image
In our daily development work, we often talk about asynchronous programming.  For example, in a user registration API, after a user successfully registers, we might send them a notification email asynchronously. But here’s a question for you:  Do you know how many different ways there are to implement asynchronous programming in Java? Let me walk you through the following commonly used approaches: • Using Thread and Runnable • Using thread pools provided by Executors • Using a custom thread pool • Using Future and Callable • Using CompletableFuture • Using ForkJoinPool • Using Spring's @Async for asynchronous methods • Using Message Queues (MQ) for async processing 1. Using Thread and Runnable Thread and Runnable are the most basic ways to implement asynchronous programming in Java.  You can directly use them to create and manage threads. public class Test { public static void main ( String [] args ) { System . out . println ( "Main ...

The Use of Lambda Expressions in Java Development

Image
1. So, what the heck is a Lambda expression? To put it simply, a Lambda expression is like a “laziness booster” that Java 8 gifted us hardworking developers. It lets you write cleaner, more elegant code.  In plain terms: it kicks out those long-winded, smelly anonymous inner classes and gives your code-base a fresh, minimalist vibe. Especially when you’re wrangling collections like `List`, `Map`, and the like—once you bring in a Lambda, your code shrinks in half and suddenly feels way more readable. Here's an example w ithout Lambda (Traditional Java approach) :  List < String > names = Arrays . asList ( "Alice" , "Bob" , "Charlie" ); // Print names in uppercase (before Java 8) for ( String name : names ) { System . out . println ( name . toUpperCase ()); } Here's an example w ith Lambda (Java 8 or later) :  List < String > names = Arrays . asList ( "Alice" , "Bob" , "Charlie" ); // Pr...

When the data volume reaches the scale of millions or tens of millions, how can a leaderboard be implemented by Java?

Image
  Introduction Over the years, many development teams stumbled when trying to implement leaderboard functionality. Today, I'd like to share several different leaderboard Java implementation strategies — ranging from simple to complex, from single-machine solutions to distributed architectures — in the hope of helping you make better decisions in real-world projects. Some java developers might think, "Isn't it just a leaderboard? Just sort the data in the database!" But in reality, it's far more complicated than that. When the data volume reaches millions or even tens of millions, a simple database query can easily become the system's bottleneck. In the following sections, I’ll walk you through several approaches in detail. Hopefully, this will be useful to you. Direct Database Sorting Use Case: Small data volume (less than 100,000 records), low real-time requirements This is the simplest and most straightforward solution — it's usually the first method th...