Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8320683

Threads created by Executors.newVirtualThreadPerTaskExecutor are not named

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "21.0.1" 2023-10-17 LTS
      OpenJDK Runtime Environment Temurin-21.0.1+12 (build 21.0.1+12-LTS)
      OpenJDK 64-Bit Server VM Temurin-21.0.1+12 (build 21.0.1+12-LTS, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      Executors.newVirtualThreadPerTaskExecutor didn't name the threads it created by default, which caused great inconvenience to logging and debugging. Moreover, it does not provide a method for customizing thread factory.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. create an executor by Executors.newVirtualThreadPerTaskExecutor()
      2. submit tasks and print thread names


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      named virtual threads, for example:

      Thread: (0) virtual-0
      Thread: (1) virtual-1
      Thread: (2) virtual-2
      Thread: (3) virtual-4
      Thread: (4) virtual-3
      Thread: (6) virtual-7
      Thread: (7) virtual-9
      Thread: (9) virtual-8
      Thread: (5) virtual-5
      Thread: (8) virtual-6
      ACTUAL -
      Thread: (0)
      Thread: (1)
      Thread: (2)
      Thread: (3)
      Thread: (4)
      Thread: (6)
      Thread: (7)
      Thread: (9)
      Thread: (5)
      Thread: (8)

      ---------- BEGIN SOURCE ----------
      import java.time.Duration;
      import java.util.ArrayList;
      import java.util.concurrent.ExecutionException;
      import java.util.concurrent.Executors;
      import java.util.concurrent.Future;

      public class Main {
          public static void main(String[] args) {
              try (final var pool = Executors.newVirtualThreadPerTaskExecutor()) {
                  final var futures = new ArrayList<Future<Integer>>();
                  for (int i = 0; i < 10; i += 1) {
                      final var id = i;
                      final var future = pool.submit(() -> {
                          try {
                              Thread.sleep(Duration.ofSeconds(4));
                          } catch (InterruptedException e) {
                              throw new RuntimeException(e);
                          }
                          System.out.printf("Thread: (%d) %s%n", id, Thread.currentThread().getName());
                          return 0;
                      });
                      futures.add(future);
                  }
                  futures.forEach(future -> {
                      try {
                          future.get();
                      } catch (InterruptedException | ExecutionException e) {
                          throw new RuntimeException(e);
                      }
                  });
              }
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: