Collaboration Library in Java – quasar
1、 Synergetic process
A process can produce many threads. Each thread has its own context. When we use multithreading, if there are long-term I / O operations, the thread will always be blocked. At this time, many threads will be idle, which will cause a waste of thread resources. This is the scenario where the synergetic process is applicable.
In fact, a collaboration process is that there is a general scheduler in a thread. For multiple tasks, only one task is executing at the same time. However, once the task enters the blocking state, the task is set to suspend and run other tasks. When running or suspending other tasks, check the status of the tasks to be run or suspended to continue execution.
Coprocessing is more used for blocking intensive (such as I / O) operations, and it is more reasonable to use threads for computing intensive operations.
There is no official JAVA collaboration library. But the great community provides an excellent library, which is quasar.
Openjdk created the loom project in 2018, which is the official solution for Java to deal with the collaborative process. However, there is no complete release date yet. Java users can look forward to it.
2、 Introduction to quasar
Quasar provides high-performance lightweight threads, go like channels, Erlang actors, and other asynchronous programming tools, which can be used in Java and kotlin programming languages.
The main contribution of quasar is to provide the implementation of lightweight threads - fiber. The function and use of fiber are similar to thread, and the API interface is also similar, so it has no sense of violation. However, they are not managed by the operating system. They are scheduled by one or more forkjoinpools. A free fiber only takes up 400 bytes of memory and less CPU during switching. There can be millions of fibers in your application. Obviously, thread can't do this.
Fiber is especially suitable for replacing the code of asynchronous callbacks. Using fiberasync asynchronous callback is very simple, with good performance and higher scalability.
So why do we call quasar a collaborative library? In fact, the implementation of quasar is to find a way to stop the running thread stack so that the scheduler of quasar can intervene. There are only two conditions for JVM thread interruption: one is throwing an exception; The other is return. Here, quasar is achieved by suspending execution, which completes the implementation of the collaborative process in the form of threads.
3、 Quasar configuration
First, we need to be in POM The jar package of quasar is introduced into XML (version 0.8.0 supports jdk11 or higher):
<dependency>
<groupId>co.paralleluniverse</groupId>
<artifactId>quasar-core</artifactId>
<version>0.7.4</version>
<classifier>jdk8</classifier>
</dependency>
Quasar fiber relies on Java instrumentation to modify your code. It can be implemented through Java agent at runtime or ant task at compile time.
It is very simple to use Java agent. When the program starts, add the following instructions to the command line. Pay attention to the path to quasar jar Jar is replaced by your actual quasar Java address:
-javaagent:path-to-quasar-jar.jar
-javaagent:C:\Users\Administrator\.m2\repository\co\paralleluniverse\quasar-core\0.7.3\quasar-core-0.7.3.jar
For maven, you can use the plug-in Maven dependency plugin, which will set a property for each of your dependencies to be referenced elsewhere. We mainly want to use ${co.parallel universal: quasar core: jar}
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>getClasspathFilenames</id>
<goals>
<goal>properties</goal>
</goals>
</execution>
</executions>
</plugin>
Then you can configure exec Maven plugin or Maven surefire plugin with agent parameters, and you can use quasar when executing Maven tasks.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<!-- Turn off before production -->
<argLine>-Dco.paralleluniverse.fibers.verifyInstrumentation=true</argLine>
<!-- Enable if using compile-time (AoT) instrumentation -->
<!-- argLine>-Dco.paralleluniverse.fibers.disableAgentWarning</argLine -->
<!-- Quasar Agent for JDK 7 -->
<!-- argLine>-javaagent:${co.paralleluniverse:quasar-core:jar}</argLine-->
<!-- Quasar Agent for JDK 8 -->
<argLine>-javaagent:${co.paralleluniverse:quasar-core:jar:jdk8}</argLine>
</configuration>
</plugin>
A quasar Maven archetype is officially provided. You can generate a quasar application prototype through the following command:
git clone https://github.com/puniverse/quasar-mvn-archetype
cd quasar-mvn-archetype
mvn install
cd ..
mvn archetype:generate -DarchetypeGroupId=co.paralleluniverse -DarchetypeArtifactId=quasar-mvn-archetype -DarchetypeVersion=0.7.4 -DgroupId=testgrp -DartifactId=testprj
cd testprj
mvn test
mvn clean compile dependency:properties exec:exec
If you use gradle, you can take a look at the gradle project template: quasar gradle template project.
For detailed configuration, refer to specifying the Java agent with Maven
4、 Quasar usage
The core of quasar is the fiber class. Fiber inherits from future and has a return value. The type is generic v. the use of fiber is similar to thread,
new Fiber<Void>() {
@Override
protected Void run() throws SuspendExecution,InterruptedException {
System.out.println("Hello Fiber");
return null;
}
}.start();
You can pass suspendablerunnable or suspendablecallable to the constructor of fiber:
new Fiber<>(() -> {
System.out.println("Hello Fiber");
return null;
}).start();
5、 Introduction to comsat
What is comsat?
Comsat is also an open source library integrated with quasar provided by parallel universe, which can provide web or enterprise level technologies, such as HTTP services and database access.
Comsat is not a web framework. It does not provide a new API, but provides the integration of quasar fiber for existing technologies such as servlet, jax-rs, JDBC, etc.
It contains many libraries, such as spring, Apache httpclient, okhttp, undertow, netty, Kafka, etc.
http://docs.paralleluniverse.co/quasar/
Fiber Library in Java - quasar
Continue to learn about Java's fiber Library - quasar