Java’s multithreading mechanism essentially accomplishes two things, asynchronous computation and concurrency. Concurrency is solved by a series of APIs that address thread safety; asynchronous computation, on the other hand, is commonly used with Runnable and Callable in conjunction with threads.
FutureTask is a cancelable asynchronous callable API based on the Runnable implementation.
Basic usage Future represents the result of an asynchronous computation, and is executed via the ExecutorService’s Future<? > submit(Runnable task) method of the ExecutorService, which is used as the return value.
The essence and difference between IO and NIO in Java
Introduction The full name of IO is input output, is the bridge between the java program and the outside world, IO refers to all the classes in the java.io package, they exist from java 1.0. NIO is called new IO, is the new generation of IO introduced in java 1.4.
What is the nature of IO? What is the difference between it and NIO? How do we learn IO and NIO?
OpenJDK proposal will provide Java class file API
The Java community is working on a Classfile API proposal that aims to provide an API for parsing, generating, and converting Java class files; initially as an internal replacement for the ASM in the JDK, and later as a public API. It is planned that ASM will eventually be removed from the JDK entirely.
The proposal notes that class file generation, parsing, and detection are ubiquitous in the Java ecosystem; many tools and libraries need to be able to handle class files, and frameworks often perform on-the-fly bytecode instrumentation, transformation, and generation.
Building an infinite stream in java 8
Introduction In java, we can convert a specific set into a stream, so in some cases, such as in a test environment, we need to construct a stream with a certain number of elements, how do we need to handle it?
Here we can construct an unlimited stream and then call the limit method to limit the number returned.
Basic use Let’s start with an example of using Stream.iterate to create an infinite Stream.
Customizing parallelStream's thread pool
Introduction By default, ForkJoinPool creates a thread for each processor, and parallelStream will use this shared thread pool to submit tasks if not specified.
So how do we handle a specific situation where we want to use a custom ForkJoinPool?
Common operations If we want to do an addition from 1 to 1000, we can use parallel stream like this.
1 2 3 4 5 List<Integer> integerList= IntStream.range(1,1000).boxed().collect(Collectors.toList()); ForkJoinPool customThreadPool = new ForkJoinPool(4); Integer total= integerList.
Cloud Native Compiler - Java Cloud Compiler
Azul has introduced a new cloud-native compiler, Java Cloud Compiler, which offloads Java JIT compilation from local systems to elastic resources, reducing the amount of resources needed to run applications and shortening the time to peak performance.
Understanding JIT and AOT The Java Runtime is a completely self-reliant system designed to run and improve code on a single system. The runtime works through just-in-time (JIT) compilation, using native resources that convert Java bytecode (JAR and class files) into native code to improve speed and memory.
Java Virtual Threads
Virtual threading is something I’m really excited about. It’s a long-awaited feature in the language and we’ve made very few attempts to implement it in the past, but it’s finally here and it’s been merged into Java 19. This means that it will be included as a preview feature and we will be able to use it as soon as JDK 19 is released.
Most of the content on the virtual (green) threads is written by non-Java developers, why is that?
How to avoid memory leaks in java programs
Although jvm has a garbage collection mechanism, if the program is written without paying attention to some specific rules, it can still lead to memory leaks in java programs, which may end up with OutOfMemory exceptions.
1. Causes of memory leaks in Java Objects in java are divided into 2 types in terms of usage, referenced and unreferenced. Garbage collection only recovers objects that are not referenced. Referenced objects, even if they are no longer used, are not recycled.
ElasticSearch new version of JavaClient using the introduction
The java client used by ElasticSearch before version 7.17 was the Java REST Client, but since version 7.17, the Java REST Client has been officially marked as deprecated and the new version of the Java Client is recommended. This article introduces the new version of the ElasticSearch Java Client’s basic usage. The specific code reference Example project.
I. Overview Elasticsearch 7.17 version of the new Java API Client has the following advantages.
Java virtual thread (LOOM) and thread fairness
Project Loom ( JEP 425 ) is probably one of the most anticipated additions to Java ever. Its implementation of virtual threading (or “green threading”) ensures that developers can create highly concurrent applications, such as those with hundreds of thousands of open HTTP connections, adhering to the well-known thread-per-request programming model without resorting to less familiar and often more complex reactive methods.
Only recently, after several years of effort, has Loom been merged into the main line of the OpenJDK and made available as a preview feature in the latest Java 19 early access release.
Java introduces static images to solve problems like slow running
In a recent social media post, Mark Reinhold, Chief Architect of Oracle’s Java Platform Group, officially announced the launch of the “Leyden” project. The project is said to have been approved and will alleviate slow startup and performance issues by introducing “static images” on the Java platform, designed to accelerate the creation of Java businesses.
It is understood that the “Leyden” project was first proposed around May 2020 and has been discussed for more than 2 years now.
Use of CyclicBarrier in java concurrency
Introduction CyclicBarrier is a thread-safe component introduced in java 5. It has the concept of a barrier, which is used to wait for all threads to finish executing before performing a specific operation.
If we have many threads and each thread computes some data, then we need to wait for all the threads to finish executing and then add up the data computed by each thread to the final result, then we can use CyclicBarrier.
Use of CountDownLatch in java concurrency
Introduction In java concurrency, it is very important to control access to shared variables, sometimes we also want to control the order of execution of concurrent threads, for example: wait for all threads to finish executing before executing another thread, or wait for all threads to be ready before starting the execution of all threads, This time we can use CountDownLatch.
CountDownLatch contains a counter that is placed in the QueuedSynchronizer.
Java stream common operations on streams
Introduction As we all know, starting from Java8, jdk has added a new Stream class to complement the collection class.
Stream provides a higher-order abstraction of Java collection operations and expressions using an intuitive way similar to querying data from a database with SQL statements.
This style treats the collection of elements to be processed as a stream, which travels through the pipeline and can be processed at the nodes of the pipeline, such as filtering, sorting, aggregating, etc.
High-performance collections EnumMap and EnumSet
This article introduces EnumMap and EnumSet, which is a collection class under the java.util package.
The Map and Set structures are especially used in our daily work, and often used to store data or pass parameters, but there is no way to control the data setting of Map, we have no idea what kind of data others will put into it, or if the type and number of data Key of our Map is fixed in some scenarios, then in this case How can we improve the security and performance of the system?
Java Discovers Annual Encryption Vulnerability
Recently, security researcher Khaled Nassar made public on GitHub the PoC code for a newly disclosed digital signature bypass vulnerability in Java, tracked as CVE-2022-21449.
The vulnerability is known to have been discovered by Neil Madden, a researcher at security consulting firm ForgeRock, in November of last year, and was notified to Oracle the same day.
Although Oracle gave the vulnerability a CVSS rating of 7.5, ForgeRock said they had privately disclosed the vulnerability when it was first discovered and rated the vulnerability a 10 on the CVSS.
JDK 19 / Java 19 has proposed two features
JDK 18/Java 18 has been released last month, according to the plan JDK 19 will be released in September this year, a few days ago the official proposed two features that will be released in the next version. One is the Vector API, and the other is a port of the JDK to RISC-V. The former Vector API is an API that expresses vector computation and will be incubated for the fourth time in the next Java release; the latter proposes a feature to port the JDK to the open source Linux/RISC-V instruction set architecture (ISA).
Thread-Affinity for CPU-bound threads
Introduction In modern computer systems, there can be multiple CPUs, and each CPU can have multiple cores. In order to take full advantage of the capabilities of modern CPUs, JAVA introduced multithreading, where different threads can run on different CPUs or different CPU cores at the same time. However, for JAVA programmers it is possible to control how many threads are created, but which CPU the threads are running on is generally difficult to know.
Oracle fixes Java annual encryption vulnerability affecting Java 15 and above
Oracle pushed out a security update yesterday to fix a vulnerability that allowed attackers to forge certain types of SSL certificates and handshakes, two-factor authentication information, and authorization credentials generated by a range of widely used open standards. This allows an attacker to easily digitally sign files and other data.
This vulnerability affects the implementation of ECDSA (Elliptic Curve Digital Signature Algorithm) in Java 15 and above. ECDSA is an algorithm that uses elliptic curve cryptography principles to digitally authenticate messages.
The Father of Java: Don't Stick with Java 8
James Gosling, the father of Java, recently twitter that developers should abandon JDK 8 as soon as possible and opt for JDK 17 LTS, which brings huge improvements in every aspect.
But maybe the father of Java was just promoting Corretto, the OpenJDK distribution for his boss AWS. and he suggests that developers still using JDK 8 might consider using Corretto’s new version- -Includes all the latest updates and CVE defenses.