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.

How to estimate throughput and thread pool size

Estimating Throughput Now there is a task which execution time is divided into 2 parts, the first part does math and the second part waits for IO. these two parts are called compute operation and wait operation. So now we need to estimate the peak throughput that can be achieved by executing this task with the CPU at full power. Then we need to know how much time it takes to execute this task, how much time is spent on the computation part and how much time is spent on the wait part.

Changes to the String object in Java 9

Overview In Java 9, the underlying String object has been changed from char[] to byte[], which has the immediate benefit of being more memory-efficient, hence the name Compact Strings Improvement. Because in Java char takes up 2 bytes and byte takes up 1 byte, while a Unicode character does not necessarily need 2 bytes to be represented, at least ASCII characters only need 1 byte to be done. That is, if your string is full of ASCII characters, half the space would be wasted if you use char.

Java generates a lot of zombie process problems

Recently I encountered a problem, a Java process using Runtime().exec() to execute script files, creating a large number of zombie processes, while the Java process is running in the container. At that time, I saw this situation on the Host machine, and I could see that there were a large number of zombie processes. Locate the zombie process by ps aux | grep Z. 1 2 3 4 5 6 7 8 9 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 2518 0.

Linux 5.18 has two key improvements to exFAT file system support

According to the foreign media Phoronix presentation: This week’s Linux 5.18 merge window has two driver PRs for the Microsoft exFAT file system, both of which are very important driver patches, albeit small in number. exFAT on Linux 5.18+ adds an option to allow access to paths with trailing points. Until now, the exFAT driver unconditionally removed trailing dots from path components, while Linux 5.18 introduced the exFAT “keep_last_dots” mount option, which can be used to control whether trailing dots are removed.

Practical application scenarios for pipeline stream

Preface PipedInputStream and PipedOutputStream are designed to solve cross-thread byte data transfers. They always come in pairs and can only be used on two different threads, using piped input and output streams in one thread can cause deadlocks. In some business scenarios, using pipeline streams will increase the speed of file uploads and reduce CPU and IO overhead, this is very practical, so this article talks about Practical application scenarios for pipeline stream.

Principle of implementation of atomic operations

1. Introduction Atomic means “the smallest particle that cannot be further divided”, and atomic operation means “an operation or series of operations that cannot be interrupted”. Implementing atomic operations on a multiprocessor becomes a bit complicated. In this article, let’s talk about how atomic operations are implemented on Intel processors and in Java. 2. Definition of Terms Compare and Swap CAS operations require two values to be entered, an old value (the value before the desired operation) and a new value, during which the old value is compared to the new value if it has not changed, and not exchanged if it.

Spring Framework RCE Solution

Just yesterday Spring officially announced a Spring Framework RCE vulnerability CVE-2022-22965. Upgrading to Spring Framework 5.3.18+ or 5.2.20+ remains the main official Spring recommendation, which officially claims to address the root cause and prevent some other vulnerability attacks, and these also provide fixes for other CVEs. Although the vulnerability is not in Tomcat itself, the Apache Tomcat team has also released versions 10.0.20, 9.0.62 and 8.5.78 with it, which are the official Tomcat solutions for the reported CVE-2022-22965 vulnerability, which has been addressed in the above versions, for older The above version has resolved the CVE-2022-22965 vulnerability, and for older, unsupported versions of Spring Framework, the vulnerability can be circumvented by upgrading the Tomcat version.

Thread pool rejection policy

Preface When it comes to java thread pools nothing is more familiar than the ExecutorService interface. jdk1.5 adds java.util.concurrent package under this api, which greatly simplifies the development of multi-threaded code. Whether you use FixedThreadPool or CachedThreadPool the implementation behind it is ThreadPoolExecutor. threadPoolExecutor is a typical product of cache pooling design, because the pool has a size, when the pool volume is not enough to carry, it involves a rejection policy.

Spring Framework RCE, Early Announcement

Updates: [15:40 BST] Spring Boot 2.6.6 is available. [14:38 BST] Spring Boot 2.5.12 is available. [14:00 BST] CVE-2022-22965 is published. [13:03 BST] Added section “Misconceptions”. [12:34 BST] Added section “Am I Impacted”. [12:11 BST] Fix minor issue in the workaround for adding disallowedFields . [11:59 BST] Spring Framework versions 5.3.18 and 5.2.20 , which address the vulnerability, are now available. The release process for Spring Boot is in progress. Overview I would like to announce an RCE vulnerability in the Spring Framework that was leaked out ahead of CVE publication.

Java reflection mechanism

What is Reflection In the runtime state, for any class, you can know all the properties and methods of the class; for any object, you can call any of its methods and properties; this dynamically obtained information and dynamic invocation of the object’s methods is called the reflection mechanism of the Java language. So what can we do with reflection? Analyze classes at runtime. Look at objects at runtime. We can also use reflection to write a toString method for all classes to use.