Basic use of ThreadPool

Preface In Java, threads are the counterpart of system threads and are used to handle a range of system resources. The number of threads that can be opened is limited in both windows and linux, so if you create unlimited threads in your java program, you will encounter a situation where no threads can be created. CPU cores are limited and if there are multiple threads running at the same time, the CPU will rotate according to the priority of the threads and allocate a specific amount of CPU time to each thread.

The fork join framework in java

Preface Fork join framework is the introduction of java 7 framework, the introduction of this framework is mainly to improve the ability of parallel computing. Fork join has two main steps, the first is fork, a large task into many small tasks, the second is join, the results of the first task join up to generate the final result. If there is no return value in the first step, join will wait until all the small tasks are finished.

Daemon Thread in Java

Preface There are two types of threads in java, user threads and daemon threads. User threads are high priority threads and the JVM will wait for all the User Threads to finish running before it finishes running. daemon threads are low-priority threads that serve User Threads. Because daemon threads are low priority and serve only user threads, the JVM will automatically exit when all user threads are finished, regardless of whether there are still daemon threads running.

Notes on upgrading from Java 8 to Java 11

Preface Although the latest version of Java has evolved to Java 18, most projects on the market are still using Java 8. Many people are apprehensive about upgrading their Java version because the Java API is not necessarily forward compatible from Java 8. Java 11 is the next long-supported version of Java 8, and there is no doubt that Java 11 is even better than Java 8. superior. This article describes the code checking tools used to convert code from Java 8 to Java 11, as well as the problems you may encounter and suggestions for resolving them.

AbstractQueuedSynchronizer implementation principle - 2

Preface In the previous articleAbstractQueuedSynchronizer implementation principle - 1, we explained the acquisition and release of exclusive synchronization state in AbstractQueuedSynchronizer, and here we start to explain the acquisition and release of shared synchronization state in AbstractQueuedSynchronizer. Shared Synchronous State Acquisition and Release Shared lock as the name implies is that multiple threads can share a lock, use acquireShared in the synchronizer to get the shared lock (synchronous state), the source code of the method is as follows.

AbstractQueuedSynchronizer implementation principle - 1

Preface AbstractQueuedSynchronizer is the basic framework for implementing concurrency tools in the JDK, and a deeper understanding of it will help us to better use its features and related tools. We hope you will read this article carefully and gain something from it. In Java, access to shared resources by multiple threads is controlled by lock. We know that the lock function can be implemented by the synchronized keyword, which can implicitly acquire locks, that is, we do not need to care about the process of acquiring and releasing locks by using this keyword, but while it provides convenience, it also means that its flexibility is reduced.

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.