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

Provides the Thread#startVirtualThread overloaded method

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      ADDITIONAL SYSTEM INFORMATION :
      openjdk version "19-ea" 2022-09-20
      OpenJDK Runtime Environment (build 19-ea+34-2229)
      OpenJDK 64-Bit Server VM (build 19-ea+34-2229, mixed mode, sharing)

      A DESCRIPTION OF THE PROBLEM :
      In swing or javafx, the update of the control needs to be done in the specified thread. Currently, there is no way to set the Executor in the way of creating VirtualThread (or I missed it), so I hope to provide an override function for setting the Executor. Thread#startVirtualThread(Executor, Runnable), currently I can only use reflection for testing

      //swing scheduler
      class SwingExecutor implements Executor{

      @Override
      public void execute(Runnable command) {
      EventQueue.invokeLater(command);
      }
      }

      //create Virtual Thread
      static Thread newVirtualThread(Executor scheduler, Runnable task){
      try {
      Class<?> threadBuilders = Class.forName("java.lang.ThreadBuilders");
      Method method = threadBuilders.getDeclaredMethod("newVirtualThread",
      Executor.class, String.class,
      int.class, Runnable.class);
      method.setAccessible(true);
      return (Thread) method.invoke(null, scheduler, null, 0, task);
      } catch (Exception e) {
      e.printStackTrace();
      }
      return null;
      }


      Attachments

        Issue Links

          Activity

            People

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

              Dates

                Created:
                Updated:
                Resolved: