How to realize the retrospective version of the JSR 166 framework in the Java class library
JSR 166 (Java Specification Request 166) is a specification on the Java platform to provide high -performance, scalable concurrent programming libraries.The back retrospective version is a function of developers who use the older Java versions in the early days so that they can use the JSR 166 framework in these versions.This article will introduce how to realize the retrospective of the JSR 166 framework in the Java library and provide the corresponding code example.
To achieve the retrospective of the JSR 166 framework in the Java class library, you can use the "JSR166Y" class library developed by Doug Lea. It provides the early JSR 166 concurrent retrospective function for the early Java version.Here are the steps to realize the retrospective version:
Step 1: Download jar package
You can download the "jsr166" jar package from the Maven warehouse (https://mvnrepository.com/artifact/com.googleCode.jsr166) to add it to the classpath of your project.
Step 2: Import the required classes
Introduce the required classes, such as the class under the `java.util.concurrent` package, which contain the concurrent tools in the JSR 166 framework.
import java.util.concurrent.*;
Step 3: Use the backback class and interface
Using the retrospective class and interfaces in the "JSR166Y" class library, they have the same function as the new version concurrent classes provided by the Java platform, but can be used in the old version of Java.
For example, you can use the retrospective version of the `Executorservice` and` CompletionService` interface to implement parallel tasks:
ExecutorService executorService = Executors.newFixedThreadPool(5);
CompletionService<Integer> completionService = new ExecutorCompletionService<>(executorService);
This enables you to use concurrent tasks to execute and obtain the characteristics of execution results in the old version of Java.
Step 4: Written concurrent code
You can use the concurrent tools provided by the retrospective class library to write a concurrent code, such as `Lock`,` Condition`, `Semaphore`,` Countdownlatch`, etc.
The following is a simple example of using a back -back class library:
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class BackportExample {
private static Lock lock = new ReentrantLock();
public static void main(String[] args) {
new Thread(() -> {
lock.lock();
try {
System.out.println ("Thread 1 obtained locks");
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
System.out.println ("Thread 1 releases locks");
}
}).start();
new Thread(() -> {
lock.lock();
try {
System.out.println ("Thread 2 obtained locks");
} finally {
lock.unlock();
System.out.println ("Thread 2 releases locks");
}
}).start();
}
}
The above code creates a re -incoming lock and demonstrated the acquisition and release operation of the lock through two threads.
By using the JSR 166 framework, developers can enjoy the convenience of concurrent programming in the early version of Java.When introducing the retrospective version, ensure the compatibility of the Java version and the JSR 166 retrospective version of the application.
Summarize:
This article introduces how to achieve the retrospective of the JSR 166 framework in the Java library.We provide the corresponding steps and give a simple sample code that uses the retrospective library.Through these contents, developers can achieve high -performance, scalable concurrent programming in the older version of Java.