Configure virtual threads in Cloud Client Libraries for Java
Stay organized with collections
Save and categorize content based on your preferences.
Virtual threads, made generally available in JDK 21, simplify the development of high concurrency applications. Some key benefits may include:
Higher throughput, especially in applications with blocking I/O operations.
Memory optimization due to their lightweight nature.
Simple concurrency model where the applications can be represented in a thread-per-request style.
Before applying virtual threads to your application, see the Open JDK documentation's JEP 444: Virtual Threads for a detailed description on both the benefits and limitations of this feature.
To make sure Java Development Kit (JDK) 21 or later is installed, run the java -version command and verify that you see the following output. For steps to install JDK, see Setting Up a Java Development Environment.
openjdk version "21.0.2" 2024-01-16 LTS
OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)
OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)
Use the virtual thread executor in the client libraries
When you instantiate a client library class, you can choose its transport implementation (gRPC and REST).
REST transport
The client libraries that implement REST transport use blocking network I/O calls, which benefit from the throughput improvements provided by virtual threads. The following code snippet demonstrates how blocking network I/O traffic in the Cloud Client Libraries for Java can be managed by virtual threads. You can implement this in your own application by overriding the default executor with a virtual thread executor in a TransportChannelProvider instance.
Since gRPC already uses asynchronous logic to handle requests, throughput improvements from virtual threads may be less pronounced. However, they can still provide some memory optimization benefits due to their lightweight nature. The following code snippet demonstrates how gRPC transport operations can be managed by virtual threads. You can implement this in your own application by overriding
the default executor with a virtual thread executor in a TransportChannelProvider instance.
importcom.google.cloud.translate.*;importjava.util.concurrent.Executors;ExecutorServicevirtualThreadExecutor=Executors.newVirtualThreadPerTaskExecutor();InstantiatingGrpcChannelProvidergrpcChannelProvider=TranslationServiceSettings.defaultGrpcTransportProviderBuilder()// Use virtual threads for gRPC transport operations..setExecutor(virtualThreadExecutor).build();try(TranslationServiceClientclient=TranslationServiceClient.create(TranslationServiceSettings.newBuilder().setTransportChannelProvider(grpcChannelProvider).build())){// Make translate requests}
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Configure virtual threads in Cloud Client Libraries for Java\n\nVirtual threads, made generally available in JDK 21, simplify the development of high concurrency applications. Some key benefits may include:\n\n- Higher throughput, especially in applications with blocking I/O operations.\n- Memory optimization due to their lightweight nature.\n- Simple concurrency model where the applications can be represented in a thread-per-request style.\n\nBefore applying virtual threads to your application, see the Open JDK documentation's [JEP 444: Virtual Threads](https://openjdk.org/jeps/444) for a detailed description on both the benefits and limitations of this feature.\n\nThis guide walks through how to configure [Cloud Client Libraries for Java](/java/docs/reference) in the [googleapis/google-cloud-java repository](https://github.com/googleapis/google-cloud-java) to use virtual threads.\n\nPrerequisites\n-------------\n\nFollow these prerequisite steps to use virtual threads with the Cloud Client Libraries for Java that you want to use in your application.\n\n1. If you have not already, create a Google Cloud Project. See [Creating and managing projects](/resource-manager/docs/creating-managing-projects).\n2. Install the [Google Cloud CLI](/sdk/docs/install), which lets you run the sample with your project's credentials.\n3. Log in with Application Default Credentials using the following command. For further instructions, see [Set up Application Default Credentials](/docs/authentication/provide-credentials-adc) :\n\n gcloud auth application-default login\n\n4. To make sure Java Development Kit (JDK) 21 or later is installed, run the `java -version` command and verify that you see the following output. For steps to install JDK, see [Setting Up a Java Development Environment](/java/docs/setup).\n\n openjdk version \"21.0.2\" 2024-01-16 LTS\n OpenJDK Runtime Environment Temurin-21.0.2+13 (build 21.0.2+13-LTS)\n OpenJDK 64-Bit Server VM Temurin-21.0.2+13 (build 21.0.2+13-LTS, mixed mode, sharing)\n\nImport the library into your project\n------------------------------------\n\nThis example uses `google-cloud-translate`. For steps to use the library in Maven or Gradle, see [Use the Cloud Translation for Java](/java/docs/reference/google-cloud-translate/latest/overview#use-the-cloud-translation-for-java)\n\nUse the virtual thread executor in the client libraries\n-------------------------------------------------------\n\nWhen you instantiate a client library class, you can choose its transport implementation (gRPC and REST).\n\n### REST transport\n\nThe client libraries that implement REST transport use blocking network I/O calls, which benefit from the throughput improvements provided by virtual threads. The following code snippet demonstrates how blocking network I/O traffic in the Cloud Client Libraries for Java can be managed by virtual threads. You can implement this in your own application by overriding the default executor with a virtual thread executor in a [TransportChannelProvider](/java/docs/reference/gax/latest/com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider.Builder) instance. \n\n import com.google.cloud.translate.*;\n import java.util.concurrent.Executors;\n\n ExecutorService virtualThreadExecutor = Executors.newVirtualThreadPerTaskExecutor();\n InstantiatingHttpJsonChannelProvider httpJsonChannelProvider =\n https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceSettings.html.https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceSettings.html#com_google_cloud_translate_v3_TranslationServiceSettings_defaultHttpJsonTransportProviderBuilder__()\n // Use virtual threads for network I/O calls.\n .setExecutor(virtualThreadExecutor)\n .build();\n\n try (https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceClient.html client =\n https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceClient.html.create(\n https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceSettings.html.https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceSettings.html#com_google_cloud_translate_v3_TranslationServiceSettings_newHttpJsonBuilder__()\n .setTransportChannelProvider(httpJsonChannelProvider)\n .build())) {\n // Make translate requests\n }\n\n### gRPC transport\n\nSince gRPC already uses asynchronous logic to handle requests, throughput improvements from virtual threads may be less pronounced. However, they can still provide some memory optimization benefits due to their lightweight nature. The following code snippet demonstrates how gRPC transport operations can be managed by virtual threads. You can implement this in your own application by overriding\nthe default executor with a virtual thread executor in a [TransportChannelProvider](/java/docs/reference/gax/latest/com.google.api.gax.grpc.InstantiatingGrpcChannelProvider.Builder) instance. \n\n import com.google.cloud.translate.*;\n import java.util.concurrent.Executors;\n\n ExecutorService virtualThreadExecutor = Executors.newVirtualThreadPerTaskExecutor();\n InstantiatingGrpcChannelProvider grpcChannelProvider =\n https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceSettings.html.https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceSettings.html#com_google_cloud_translate_v3_TranslationServiceSettings_defaultGrpcTransportProviderBuilder__()\n // Use virtual threads for gRPC transport operations.\n .setExecutor(virtualThreadExecutor)\n .build();\n\n try (https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceClient.html client =\n https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceClient.html.create(\n https://cloud.google.com/java/docs/reference/google-cloud-translate/latest/com.google.cloud.translate.v3.TranslationServiceSettings.html.newBuilder()\n .setTransportChannelProvider(grpcChannelProvider)\n .build())) {\n // Make translate requests\n }"]]