Contents

How to enable virtual threads in Java 20

Writen by: David Vlijmincx

Introduction

Virtual threads and structured concurrency are both in incubation/preview. You can only use them in your project if you set some extra flags when you start or compile your code. In this article, I will tell you which flags you need to set and how to do so in IntelliJ.

Enable virtual threads in Java 20

To use virtual threads in Java, you only need to add this flag/ VM option when you run your code:

1
--enable-preview

You must change your run configuration to add this VM option in IntelliJ. You can find your configurations left of the run icon in the upper right corner.

  1. Click on the drop-down and select “Edit configurations…”
  2. Select the configuration you want to edit
  3. In the “Build and run” section, click on “Modify options”
  4. Select “Add VM options”
  5. A new textbox will be added to the configuration screen
  6. Inside the textbox, add the following text without quotes: “–enable-preview
  7. Click Apply or OK

You can use virtual threads when you run your code using this configuration.

Suppose you want to use these flags for all your (future) configurations by default. In that case, you can follow the same steps to change the configuration template. To do so in step 2, don't select a configuration but the “edit configuration template…” button in the lower-left corner of the pop-up screen.

Enable structured concurrency in Java 20

To enable structured concurrency, we have two choices. We can add a module file or add extra parameters to the compiler and run configuration.

Enable structured concurrency with a module file

The easiest way to enable structured concurrency is with a module file. To do this, you need to create a module-info.java file in your java directory. The complete path will look like this src/main/java/module-info.java

Copy and paste the content of the following example inside the module file:

1
2
3
module yourProjectName {
    requires jdk.incubator.concurrent;
}

You can now start using structured concurrency in your project, but remember to enable virtual threads!

Enable structured concurrency with compiler and run parameters

The second option to enable structured concurrency is to use parameters for the compiler and the run configuration.

Compiler parameters

These are parameters the parameters you need for the Java compiler:

1
--enable-preview --add-modules jdk.incubator.concurrent

To pass these parameters to the java compiler in IntelliJ, you have to perform these steps:

  1. select “File” in the upper left corner
  2. Click on “Settings”
  3. Open “Build, Execution, Deployment”
  4. Open “Compiler”
  5. Click on “Java Compiler”
  6. In the lower portion of the screen, you see a textbox called “Additional command line parameters:”
  7. Paste the following text without quotes: “–enable-preview –add-modules jdk.incubator.concurrent
  8. Click Apply or OK

Run parameters

We also need to add the flags/ options to the run configuration.

  1. Click on the drop-down and select “Edit configurations…”
  2. Select the configuration you want to edit
  3. In the “Build and run” section, click on “Modify options”
  4. Select “Add VM options”
  5. A new textbox will be added to the configuration screen
  6. Inside the textbox add the following text without quotes: “–enable-preview –add-modules jdk.incubator.concurrent
  7. Click Apply or OK

Conclusion

In this article, we looked at how to enable virtual threads and structured concurrency for our project. We went over the different parameters you need to pass and how to set these parameters in IntelliJ.

Further reading

More about virtual threads in Java:

 




Questions, comments, concerns?

Have a question or comment about the content? Feel free to reach out!