The difference between Runnable and Callable in java

Preface In java’s multi-threaded development Runnable has always been the core of multi-threading, and Callable is an enhanced version of java 1.5 added in. In this article, we will explore the differences between Runnable and Callable in detail. Operation mechanism First look at the interface definition of Runnable and Callable: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 @FunctionalInterface public interface Runnable { /** * When an object implementing interface <code>Runnable</code> is used * to create a thread, starting the thread causes the object's * <code>run</code> method to be called in that separately executing * thread.

The use of ExecutorService in java concurrency

Preface ExecutorService is a framework for asynchronous execution in java. By using ExecutorService you can easily create a multi-threaded execution environment. This article will explain in detail the specific use of ExecutorService. Creating an ExecutorService Generally speaking, there are two ways to create an ExecutorService. The first way is to use the factory class methods in Executors, for example. 1 ExecutorService executor = Executors.newFixedThreadPool(10); In addition to the newFixedThreadPool method, Executors contains a number of methods to create an ExecutorService.

Java Class File Format Analysis

Preface Learn jvm word code, you need to understand the way class is composed, this article explains the process of parsing the class file format in java method, I hope it can help you. Class file specification The code that is compiled and executed by the Java virtual machine uses a platform-neutral (hardware- and operating system-independent). It is often (but not always) stored as a file, so this format is called the Class file format.

Java Virtual Threads Explained

Many languages have technologies like virtual threads, such as Go, C#, Erlang, Lua, etc, which they call concurrent threads. Whether they are virtual threads or concurrent threads, they are all lightweight threads that aim to improve concurrency. This section details the Java platform’s virtual threads technology, JEP 425: Virtual Threads (Preview). The Java platform plans to introduce virtual threads that will significantly reduce the effort of writing, maintaining, and observing high-throughput concurrent applications.

The use of Future in java

The use of Future in java Future is an interface introduced in java 1.5 that can be used to get asynchronous results easily. This article will explain how to use Future through specific examples. Creating Future As mentioned above, Future represents the result of asynchronous execution, which means that when the asynchronous execution is finished, the returned result will be saved in Future. So when do we use Future? Generally speaking, when we execute a long-running task, using Future allows us to temporarily deal with other tasks, and then return the result when the long task is finished.

Better use of Java thread pool

Introduction This article combines the JUC package provided by Doug Lea in JDK1.5 to understand the use of thread pools from the setting of thread pool size parameters, the creation of work threads, the recycling of idle threads, the use of blocking queues, task rejection strategies, thread pool Hook and other aspects, which involves some details including the choice of different parameters, different queues, different rejection strategies, the resulting The details include the different parameters, the different queues, the choice of different rejection strategies, the resulting impact and behavior, and for better use of the thread pool.

Technology Trends Java Teams Should Watch in 2022

Java is evolving quickly, and with potential changes to the OpenJDK release cycle, it may accelerate even further. For people like Michael Rasmussen, JRebel development lead at Perforce, keeping up with these changes and understanding their impact on development; creating features that resonate with the Java development community, keeping applications up to date with the latest versions of popular Java technologies, and developing new features, improvements, and integrations for JRebel is critical.

Analysis of connection pooling principles through HiKariCP

Design and Principle The base case HiKariCP as the default connection pool of SpringBoot2 framework, claimed to be the fastest running connection pool, database connection pool and the previous two mentioned thread pool and object pool, from the principle of design are based on the pooling idea, only in the implementation of their own characteristics; first or see the basic case of HiKariCP usage. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 import com.

Synchronized keywords in java concurrency

Introduction In a multi-threaded environment, we often encounter resource competition, such as multiple threads going to modify the same shared variable at the same time, it is necessary to perform some processing of the resource access method to ensure that only one thread accesses it at the same time. Java provides the synchronized keyword to facilitate us to achieve the above operation. Why synchronized Let’s take an example where we create a class that provides a setSum method.

Difference between wait and sleep in java

Introduction In this post, we will discuss the difference between wait() and sleep() methods in java. And discuss how to use these two methods. Difference between wait and sleep wait() is a native method defined in Object. 1 public final native void wait(long timeout) throws InterruptedException; So every instance of the class can call this method. wait() can only be called in a synchronized block. It will release the lock put on the object when it is synchronized.

Advanced Usage of java - Function in JNA

Introduction In JNA, in order to map with native functions, we can have two mapping methods, the first one is interface mapping and the second one is direct mapping. Although the two approaches are different, in the specific method mapping, we both need to define a method in JAVA that maps to the native method.. This JAVA mapping is a function in JNA, and by using a function object, we can achieve some very powerful functionality.

Java Zombie Processes Continued

In a previous article, I mentioned the problem of Java program containers generating a lot of zombie processes. Today I ran into it again, but in a different way. The zombie processes are also generated by a bash script, but this script is executed in the K8S readinessProbe and livenessProbe, not by the java program itself. Let’s look at some failure diagrams. You can see that there are many timeout processes that are not cleaned up after execution.

Java introduces preview virtual threads

OpenJDK’s JEP 425: Virtual Threads (Preview) feature proposal shows that the Java platform will introduce the virtual threads feature. Virtual threads are lightweight threads that can significantly reduce the effort of writing, maintaining, and observing high-throughput concurrent applications. Java developers have always relied on threads as the building blocks of concurrent server applications, where statements in each method are executed within a thread, and each thread provides a stack to store local variables and coordinate method calls, as well as context trapping when errors are reported.

Application of Volatile

Memory Visibility Since the Java Memory Model (JMM) states that all variables are stored in main memory, and each thread has its own working memory (cache). When a thread is working, it needs to copy the data from the main memory to the working memory. This way, any operation on the data is based on the working memory (which is more efficient) and cannot directly manipulate the data in the main memory or the working memory of other threads, and then flush the updated data to the main memory afterwards.

ReentrantLock implementation principle

When using synchronize to do synchronization, lock acquisition and release are implicitly implemented by compiling and adding different machine instructions to achieve the principle. ReentrantLock is a class based on AbstractQueuedSynchronizer(AQS for short) implementation, this article analyzes the implementation principle of ReentrantLock, I hope it will be helpful to you. ReentrantLock: a thread can still repeat the lock after it has obtained it, and will not appear to block itself.

Practical Applications of Reflection - Building Log Objects

Preface In our practical development, we often use reflection, I present an example of the logging function done by reflection in practical development. Traditional logging Some basic knowledge about reflection can be found in Reflection Primer. I believe that logging is not new to you, in the actual development of some more sensitive data tables we need to record every operation of it. First, let’s look at the traditional way of writing.

How to implement detection of undirected and directed loops in Java

Undirected rings An undirected graph with rings is shown below, where there are two rings, 0-2-1-0 and 2-3-4-2, respectively. To detect rings in an undirected graph, you can use depth-first search. Suppose we start from vertex 0, then walk to the adjacent vertex 2, then walk to the vertex 1 adjacent to vertex 2. Since vertex 0 and vertex 1 are adjacent and vertex 0 is labeled, it means we have spared a loop, so there is a loop in the undirected graph.

How to implement undirected graphs in Java

Basic concepts Definition of a graph A graph is a binary consisting of a set of points V={vi}V={vi} and a set E={ek}E={ek} of unordered pairs of elements in VV, denoted G=(V,E)G=(V,E), elements vivi in VV are called vertices and elements ekek in EE are called edges. For two points u,vu,v in VV, if the edge (u,v)(u,v) belongs to EE, then the two points u,vu,v are said to be adjacent and u,vu,v is called the endpoint of the edge (u,v)(u,v).

Usage of Bigdecimal

As a Java developer in the daily work, many times we will encounter some need to calculate the data scenario, usually for the scenario does not need to calculate the precision we can use Integer, Float or Double to calculate, although will lose precision but occasionally can be used, if we need to calculate the results accurately, we will use the class BigDecimal provided in the java.math package to implement the corresponding function.

Java8 new features of the time and date API

Java8 includes a new time and date API, which is placed under the java.time package. This new time-date API is immutable and thread-safe (This class is immutable and thread-safe). Local time: LocalDate, LocalTime, LocalDateTime Instances of the classes localDate, LocalTime, and LocalDateTime are immutable objects that represent the date, time, date, and time, respectively, using the ISO-8601 calendar system. They provide a simple date or time and do not contain current time information.