Intro to Coroutines with Quasar – 使用Quasar的Coroutines介绍

最后修改: 2020年 4月 5日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

Coroutines are an alternative to Java Threads as they provide a way to execute interruptable tasks on a very high level of concurrency, but until Project Loom is complete, we have to look to library support to get it.

Coroutines是Java Threads的替代方案,因为它提供了一种在非常高的并发水平上执行可中断任务的方法,但在Project Loom完成之前,我们不得不期待库的支持来获得它。

In this tutorial, we’ll take a look at Quasar, one such library that offers co-routine support.

在本教程中,我们将看看Quasar,一个提供联合程序支持的这样一个库。

2. Setup

2.设置

We’ll use the latest version of Quasar which requires Java 11 or higher. But, the example application will also work with prior versions of Quasar which are compatible with Java 7 and 8.

我们将使用最新的Quasar版本,需要Java 11或更高版本。但是,这个例子的应用程序也可以在之前的Quasar版本中使用,这些版本与Java 7和8兼容。

Quasar provides three dependencies which we need to include in our build:

Quasar提供了三个依赖项,我们需要将其纳入我们的构建中。

<dependency>
    <groupId>co.paralleluniverse</groupId>
    <artifactId>quasar-core</artifactId>
    <version>0.8.0</version>
</dependency>
<dependency>
    <groupId>co.paralleluniverse</groupId>
    <artifactId>quasar-actors</artifactId>
    <version>0.8.0</version>
</dependency>
<dependency>
    <groupId>co.paralleluniverse</groupId>
    <artifactId>quasar-reactive-streams</artifactId>
    <version>0.8.0</version>
</dependency>

Quasar’s implementation relies on bytecode instrumentation to work correctly. To perform bytecode instrumentation, we have two options:

Quasar的实现依赖于字节码工具化才能正常工作。为了执行字节码检测,我们有两个选择。

  • At compile time, or
  • At runtime with the Java agent

Using the Java agent is the preferred way since it doesn’t have any special build requirements and works with any setup.

使用Java代理是首选的方式,因为它没有任何特殊的构建要求,可以在任何设置下工作。

2.1. Specify the Java Agent with Maven

2.1.用Maven指定Java代理

To run the Java agent with Maven, we need to include the maven-dependency-plugin to always run the properties goal:

为了用Maven运行Java代理,我们需要包含maven-dependency-plugin以始终运行properties目标。

<plugin>
    <artifactId>maven-dependency-plugin</artifactId>
    <version>3.1.1</version>
    <executions>
        <execution>
            <id>getClasspathFilenames</id>
            <goals>
               <goal>properties</goal>
            </goals>
        </execution>
    </executions>
</plugin>

The properties goal will generate a property that is pointing to the quasar-core.jar location on the classpath.

properties 目标将生成一个指向classpath上quasar-core.jar位置的属性。

For the execution of our application we’ll use the exec-maven-plugin:

为了执行我们的应用程序,我们将使用exec-maven-plugin

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>exec-maven-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <workingDirectory>target/classes</workingDirectory>
        <executable>echo</executable>
        <arguments>
            <argument>-javaagent:${co.paralleluniverse:quasar-core:jar}</argument>
            <argument>-classpath</argument> <classpath/>
            <argument>com.baeldung.quasar.QuasarHelloWorldKt</argument>
        </arguments>
    </configuration>
</plugin>

To make use of that plugin and launch our application we will then run Maven:

为了利用该插件并启动我们的应用程序,我们将运行Maven。

mvn compile dependency:properties exec:exec

3. Implementing Coroutines

3.实现轮回程序

To implement the coroutine, we’ll use Fibers from the Quasar library. Fibers provide lightweight threads that will be managed by the JVM instead of the operating system. Because they require very little RAM and put far less burden on the CPU, we could have millions of them in our application without having to worry about performance.

为了实现coroutine,我们将使用Quasar库中的FibersFibers提供了轻量级的线程,将由JVM而不是操作系统管理。因为它们只需要很少的内存,对CPU的负担也小得多,所以我们可以在我们的应用程序中拥有数百万个线程而不必担心性能问题。

To launch a fiber, we create an instance of the Fiber<T> class which will wrap the code that we want to execute and call the start method:

为了启动一个光纤,我们创建一个光纤<T>类的实例,它将包裹我们想要执行的代码并调用start方法。

new Fiber<Void>(() -> {
    System.out.println("Inside fiber coroutine...");
}).start();

4. Conclusion

4.总结

In this article, we’ve introduced how to implement coroutines by using the Quasar library. What we’ve seen here is only a minimal working example and the Quasar library is capable of doing much more.

在这篇文章中,我们介绍了如何通过使用Quasar库来实现coroutines。我们在这里看到的只是一个最小的工作实例,Quasar库能够做得更多。

Please find all of the source code over on GitHub.

请在GitHub上找到所有的源代码