< prev index next >

src/java.base/share/classes/sun/nio/ch/Poller.java

Print this page
@@ -205,11 +205,15 @@
       * Callback by the poll method when a file descriptor is polled.
       */
      final void polled(int fdVal) {
          Thread t = map.remove(fdVal);
          if (t != null) {
-             LockSupport.unpark(t);
+             if (POLLER_GROUP.useLazyUnpark() && Thread.currentThread().isVirtual()) {
+                 JLA.lazyUnparkVirtualThread(t);
+             } else {
+                 LockSupport.unpark(t);
+             }
          }
      }
  
      /**
       * Parks the current thread until a file descriptor is ready for the given op.

@@ -396,10 +400,17 @@
          /**
           * Return the write pollers.
           */
          abstract List<Poller> writePollers();
  
+         /**
+          * Return true if the unparking threads should use lazyUnpark.
+          */
+         boolean useLazyUnpark() {
+             return false;
+         }
+ 
          /**
           * Close the given pollers.
           */
          static void closeAll(Poller... pollers) {
              for (Poller poller : pollers) {

@@ -652,15 +663,18 @@
              Poller readPoller = provider().readPoller(true);
              readPollers.add(readPoller);
  
              // start virtual thread to execute sub-polling loop
              Thread carrier = JLA.currentCarrierThread();
-             Thread.ofVirtual()
+             Thread.Builder.OfVirtual builder = Thread.ofVirtual()
                      .inheritInheritableThreadLocals(false)
                      .name(carrier.getName() + "-Read-Poller")
-                     .uncaughtExceptionHandler((_, e) -> e.printStackTrace())
-                     .start(() -> subPollerLoop(readPoller));
+                     .uncaughtExceptionHandler((_, e) -> e.printStackTrace());
+             Thread thread = JLA.defaultVirtualThreadScheduler()
+                     .newThread(builder, carrier, () -> subPollerLoop(readPoller))
+                     .thread();
+             thread.start();
              return readPoller;
          }
  
          /**
           * Returns the read poller for the current carrier, starting it if required.

@@ -746,10 +760,15 @@
  
          @Override
          List<Poller> writePollers() {
              return List.of(writePollers);
          }
+ 
+         @Override
+         boolean useLazyUnpark() {
+             return true;
+         }
      }
  
      /**
       * Reads the given property name to get the poller count. If the property is
       * set then the value must be a power of 2. Returns 1 if the property is not
< prev index next >