Simple use of the Atomic class

Problem Background In a multi-threaded environment, the most common problem we encounter is synchronizing the values of variables. Since variables need to be shared across multiple threads, we must need to employ some synchronization mechanism to control them. From the previous article we know that the Lock mechanism can be used, and of course, the Atomic class we are talking about today. Here we will introduce each of the two ways.

Use of join in java

Introduction join() is a method we often use in java that sets the current thread to the WAITTING state and then waits for the calling thread to finish executing or be interrupted. Definition join() is a method defined in Thread, let’s look at his definition. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /** * Waits for this thread to die.

The life cycle of threads in java

Introduction Threads is a frequently used technology in java, this article will explain in detail the life cycle of threads in java, I hope you can give some help. The state of Thread in java There are six states of Thread in java, which are. NEW - newly created Thread, has not yet started execution RUNNABLE - Thread in runnable state, including ready to run and running. BLOCKED - Threads that are waiting for a resource lock WAITING - A thread that is waiting indefinitely for other threads to perform a specific operation TIMED_WAITING - Waiting for a certain amount of time for other threads to perform a specific operation TERMINATED - the thread has finished executing We can visualize this in a diagram.

The use of ThreadLocal in java

Preface ThreadLocal is mainly used to store data for the current thread, this data is only accessible by the current thread. When defining a ThreadLocal, we can also define specific types of objects stored in the ThreadLocal. 1 ThreadLocal<Integer> threadLocalValue = new ThreadLocal<>(); Above we have defined a ThreadLocal object that stores an Integer. To store and get the object in ThreadLocal is also very simple, using get() and set().

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.