{"id":364868,"date":"2024-05-21T02:26:22","date_gmt":"2024-05-21T02:26:22","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=364868"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=364868","title":{"rendered":"<span>Project Loom. \u041d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 Java 19 \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/21\/core\/virtual-threads.html#GUID-DC4306FC-D6C1-4BCC-AECE-48C32C1A8DAA\" rel=\"noopener noreferrer nofollow\">\u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0445, \u0442\u0435\u043c \u0447\u0442\u043e \u0443\u043c\u0435\u044e\u0442 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0445 I\/O \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 JVM \u0431\u044b\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d  \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0445\u0438\u043f\u0435 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0438\u043f\u0430 \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u0430. \u041f\u0440\u043e\u0449\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0431\u044b\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0435 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Coroutine\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b<\/a> \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 JVM.<\/p>\n<p>\u0418 \u044d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0440\u0435\u0432\u043e\u043b\u044e\u0446\u0438\u044f, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u0430\u043b\u043e \u043a\u0442\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u043b \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. \u0421\u0430\u043c\u043e API \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043d\u0435\u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0435, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043a\u043b\u0430\u0441\u0441 <code>jdk.internal.vm.Continuation<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <code>yield()<\/code> \u0438 <code>run()<\/code> \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u041d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u043e \u043d\u0435\u0433\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e, \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 JVM (\u043b\u0438\u0431\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f JPMS).<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0441\u0432\u043e\u044e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c \u043d\u0430 Java: <a href=\"https:\/\/github.com\/Anamorphosee\/loomoroutines\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/Anamorphosee\/loomoroutines<\/a>.<\/p>\n<p>\u0423 \u043c\u043d\u043e\u0433\u0438\u0445 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441, \u0433\u0434\u0435 \u043d\u0430\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0443\u0436\u043d\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u043a\u0440\u043e\u043c\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432? \u041e\u0442\u0432\u0435\u0442: \u0432\u0435\u0437\u0434\u0435, \u0433\u0434\u0435 \u043c\u044b \u043f\u0438\u0448\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 \u043a\u043e\u043b\u0431\u0435\u043a\u0430\u0445, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f GUI \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043c\u043e\u044f \u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>Java GUI App Example<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">import dev.reformator.loomoroutines.dispatcher.SwingDispatcher; import dev.reformator.loomoroutines.dispatcher.VirtualThreadsDispatcher;  import javax.imageio.ImageIO; import javax.swing.*;  import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URI; import java.time.Duration; import java.util.regex.Pattern;  import static dev.reformator.loomoroutines.dispatcher.DispatcherUtils.*;  public class ExampleSwing {     private static int pickingCatCounter = 0;      private static final Pattern urlPattern = Pattern.compile(\"\\\"url\\\":\\\"([^\\\"]+)\\\"\");      public static void main(String[] args) {         var frame = new JFrame(\"Cats\");         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         var panel = new JPanel();         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));         var button = new JButton(\"Pick a cat\");         var imagePanel = new ImagePanel();         panel.add(button);         panel.add(imagePanel);         frame.add(panel);         frame.setSize(1000, 500);         frame.setVisible(true);          button.addActionListener(e -> dispatch(SwingDispatcher.INSTANCE, () -> {             pickingCatCounter++;             if (pickingCatCounter % 2 == 0) {                 button.setText(\"Pick another cat\");                 return null;             } else {                 button.setText(\"This one!\");                 var cachedPickingCatCounter = pickingCatCounter;                  try {                     while (true) {                         var bufferedImage = doIn(VirtualThreadsDispatcher.INSTANCE, ExampleSwing::loadCatImage);                         if (pickingCatCounter != cachedPickingCatCounter) {                             return null;                         }                          imagePanel.setImage(bufferedImage);                         delay(Duration.ofSeconds(1));                          if (pickingCatCounter != cachedPickingCatCounter) {                             return null;                         }                     }                 } catch (Throwable ex) {                     if (pickingCatCounter == cachedPickingCatCounter) {                         ex.printStackTrace();                         pickingCatCounter++;                         button.setText(\"Exception: \" + ex.getMessage() + \". Try again?\");                     }                     return null;                 }             }         }));     }      private static BufferedImage loadCatImage() {         String url;         {             String json;             try (var stream = URI.create(\"https:\/\/api.thecatapi.com\/v1\/images\/search\").toURL().openStream()) {                 json = new String(stream.readAllBytes());             } catch (IOException ex) {                 throw new RuntimeException(ex);             }             var mather = urlPattern.matcher(json);             if (!mather.find()) {                 throw new RuntimeException(\"cat url is not found in json '\" + json + \"'\");             }             url = mather.group(1);         }         try (var stream = URI.create(url).toURL().openStream()) {             return ImageIO.read(stream);         } catch (IOException ex) {             throw new RuntimeException(ex);         }     } }  class ImagePanel extends JPanel {     private BufferedImage image = null;      public void setImage(BufferedImage image) {         this.image = image;         repaint();     }      @Override     protected void paintComponent(Graphics g) {         super.paintComponent(g);         if (image != null) {             g.drawImage(image, 0, 0, null);         }     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0431\u0435\u043a\u0430, \u043a\u043e\u0434 \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435, \u043a\u0430\u043a \u0431\u0443\u0434\u0442\u043e \u0432\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0436\u0435 \u0434\u0435\u043b\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 (\u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435) \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442 UI.<\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0437\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043a\u043e\u0442\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c Kotlin, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0430? \u0422\u0443\u0442 \u044f \u043c\u043e\u0433\u0443 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e Kotlin-\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0438 \u0438\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u0438\u043c\u0435\u044e\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441 \u0438\u0445 \u043e\u0442\u043b\u0430\u0434\u043a\u043e\u0439 <a href=\"https:\/\/stackoverflow.com\/questions\/54349418\/how-to-recover-the-coroutines-true-call-trace\" rel=\"noopener noreferrer nofollow\">(\u0432 \u043d\u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0435\u0437\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u0430)<\/a>. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, Kotlin-\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043e\u0431\u044f\u0437\u044b\u0432\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Kotlin, \u0434\u043b\u044f Loom-\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Java, Scala, Kotlin, Groovy \u0438\u043b\u0438 \u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 JVM-\u044f\u0437\u044b\u043a.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/785334\/\"> https:\/\/habr.com\/ru\/articles\/785334\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 Java 19 \u043d\u0430\u043c \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b <a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/21\/core\/virtual-threads.html#GUID-DC4306FC-D6C1-4BCC-AECE-48C32C1A8DAA\" rel=\"noopener noreferrer nofollow\">\u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438<\/a>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0445, \u0442\u0435\u043c \u0447\u0442\u043e \u0443\u043c\u0435\u044e\u0442 \u043e\u0441\u0432\u043e\u0431\u043e\u0436\u0434\u0430\u0442\u044c \u043f\u043e\u0442\u043e\u043a \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u044b \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0445 I\/O \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 JVM \u0431\u044b\u043b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d  \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0432 \u0445\u0438\u043f\u0435 \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0438\u0437 \u0445\u0438\u043f\u0430 \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u0430. \u041f\u0440\u043e\u0449\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, \u0431\u044b\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u043f\u043e\u043b\u043d\u043e\u0446\u0435\u043d\u043d\u044b\u0435 <a href=\"https:\/\/en.wikipedia.org\/wiki\/Coroutine\" rel=\"noopener noreferrer nofollow\">\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b<\/a> \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 JVM.<\/p>\n<p>\u0418 \u044d\u0442\u043e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0430\u044f \u0440\u0435\u0432\u043e\u043b\u044e\u0446\u0438\u044f, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u043c\u0430\u043b\u043e \u043a\u0442\u043e \u043e\u0431\u0440\u0430\u0442\u0438\u043b \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435. \u0421\u0430\u043c\u043e API \u0434\u043b\u044f \u0442\u0430\u043a\u0438\u0445 \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0445 \u043a\u043e\u0440\u0443\u0442\u0438\u043d \u043d\u0435\u043f\u0443\u0431\u043b\u0438\u0447\u043d\u043e\u0435, \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e \u0447\u0435\u0440\u0435\u0437 \u043a\u043b\u0430\u0441\u0441 <code>jdk.internal.vm.Continuation<\/code>, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0435\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u044b <code>yield()<\/code> \u0438 <code>run()<\/code> \u0434\u043b\u044f \u0441\u043e\u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u0430 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e. \u041d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u0434\u043e \u043d\u0435\u0433\u043e \u043d\u0435\u0441\u043b\u043e\u0436\u043d\u043e, \u043d\u0443\u0436\u043d\u043e \u043b\u0438\u0448\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0430\u0440\u0443 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 JVM (\u043b\u0438\u0431\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u043c, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u0445\u043e\u0434\u0438\u0442\u044c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f JPMS).<\/p>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u044e \u0441\u0432\u043e\u044e \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443 \u0434\u043b\u044f \u0434\u043e\u0441\u0442\u0443\u043f\u0430 \u043a \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u043c \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u043c \u043d\u0430 Java: <a href=\"https:\/\/github.com\/Anamorphosee\/loomoroutines\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/Anamorphosee\/loomoroutines<\/a>.<\/p>\n<p>\u0423 \u043c\u043d\u043e\u0433\u0438\u0445 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u0443\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441, \u0433\u0434\u0435 \u043d\u0430\u043c \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0443\u0436\u043d\u044b \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u043a\u0440\u043e\u043c\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432? \u041e\u0442\u0432\u0435\u0442: \u0432\u0435\u0437\u0434\u0435, \u0433\u0434\u0435 \u043c\u044b \u043f\u0438\u0448\u0435\u043c \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 \u043a\u043e\u043b\u0431\u0435\u043a\u0430\u0445, \u0435\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u043d\u0430 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043a\u043e\u0434 \u043d\u0430 \u043a\u043e\u0440\u0443\u0442\u0438\u043d\u0430\u0445. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f GUI \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439, \u043c\u043e\u044f \u043e\u0431\u0435\u0440\u0442\u043a\u0430 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0432\u043e\u0442 \u0442\u0430\u043a:<\/p>\n<details class=\"spoiler\">\n<summary>Java GUI App Example<\/summary>\n<div class=\"spoiler__content\">\n<pre><code class=\"java\">import dev.reformator.loomoroutines.dispatcher.SwingDispatcher; import dev.reformator.loomoroutines.dispatcher.VirtualThreadsDispatcher;  import javax.imageio.ImageIO; import javax.swing.*;  import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URI; import java.time.Duration; import java.util.regex.Pattern;  import static dev.reformator.loomoroutines.dispatcher.DispatcherUtils.*;  public class ExampleSwing {     private static int pickingCatCounter = 0;      private static final Pattern urlPattern = Pattern.compile(\"\\\"url\\\":\\\"([^\\\"]+)\\\"\");      public static void main(String[] args) {         var frame = new JFrame(\"Cats\");         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         var panel = new JPanel();         panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));         var button = new JButton(\"Pick a cat\");         var imagePanel = new ImagePanel();         panel.add(button);         panel.add(imagePanel);         frame.add(panel);         frame.setSize(1000, 500);         frame.setVisible(true);          button.addActionListener(e -> dispatch(SwingDispatcher.INSTANCE, () -> {             pickingCatCounter++;             if (pickingCatCounter % 2 == 0) {                 button.setText(\"Pick another cat\");                 return null;             } else {                 button.setText(\"This one!\");                 var cachedPickingCatCounter = pickingCatCounter;                  try {                     while (true) {                         var bufferedImage = doIn(VirtualThreadsDispatcher.INSTANCE, ExampleSwing::loadCatImage);                         if (pickingCatCounter != cachedPickingCatCounter) {                             return null;                         }                          imagePanel.setImage(bufferedImage);                         delay(Duration.ofSeconds(1));                          if (pickingCatCounter != cachedPickingCatCounter) {                             return null;                         }                     }                 } catch (Throwable ex) {                     if (pickingCatCounter == cachedPickingCatCounter) {                         ex.printStackTrace();                         pickingCatCounter++;                         button.setText(\"Exception: \" + ex.getMessage() + \". Try again?\");                     }                     return null;                 }             }         }));     }      private static BufferedImage loadCatImage() {         String url;         {             String json;             try (var stream = URI.create(\"https:\/\/api.thecatapi.com\/v1\/images\/search\").toURL().openStream()) {                 json = new String(stream.readAllBytes());             } catch (IOException ex) {                 throw new RuntimeException(ex);             }             var mather = urlPattern.matcher(json);             if (!mather.find()) {                 throw new RuntimeException(\"cat url is not found in json '\" + json + \"'\");             }             url = mather.group(1);         }         try (var stream = URI.create(url).toURL().openStream()) {             return ImageIO.read(stream);         } catch (IOException ex) {             throw new RuntimeException(ex);         }     } }  class ImagePanel extends JPanel {     private BufferedImage image = null;      public void setImage(BufferedImage image) {         this.image = image;         repaint();     }      @Override     protected void paintComponent(Graphics g) {         super.paintComponent(g);         if (image != null) {             g.drawImage(image, 0, 0, null);         }     } }<\/code><\/pre>\n<\/p>\n<\/div>\n<\/details>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043d\u0435\u0442 \u043d\u0438 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043e\u043b\u0431\u0435\u043a\u0430, \u043a\u043e\u0434 \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u0432 \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u043c \u0441\u0442\u0438\u043b\u0435, \u043a\u0430\u043a \u0431\u0443\u0434\u0442\u043e \u0432\u0441\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432 UI \u043f\u043e\u0442\u043e\u043a\u0435. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0436\u0435 \u0434\u0435\u043b\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 (\u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0430 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435) \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u044f\u0442\u0441\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0438 \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442 UI.<\/p>\n<p>\u0425\u043e\u0440\u043e\u0448\u043e, \u043d\u043e \u0437\u0430\u0447\u0435\u043c \u043d\u0430\u043c \u043d\u0430\u0442\u0438\u0432\u043d\u044b\u0435 \u043a\u043e\u0442\u043e\u0440\u0443\u0442\u0438\u043d\u044b, \u043a\u043e\u0433\u0434\u0430 \u0435\u0441\u0442\u044c Kotlin, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u043d\u0438 \u0443\u0436\u0435 \u0434\u0430\u0432\u043d\u043e \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b \u0440\u0430\u043d\u0442\u0430\u0439\u043c\u0430? \u0422\u0443\u0442 \u044f \u043c\u043e\u0433\u0443 \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c, \u0447\u0442\u043e Kotlin-\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u044b \u0441\u043b\u0438\u0448\u043a\u043e\u043c \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043e \u0438 \u0438\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u0438\u043c\u0435\u044e\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441 \u0438\u0445 \u043e\u0442\u043b\u0430\u0434\u043a\u043e\u0439 <a href=\"https:\/\/stackoverflow.com\/questions\/54349418\/how-to-recover-the-coroutines-true-call-trace\" rel=\"noopener noreferrer nofollow\">(\u0432 \u043d\u0438\u0445 \u043f\u043e\u0441\u043b\u0435 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u043e\u0431\u0440\u0435\u0437\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u0430)<\/a>. \u041a\u0440\u043e\u043c\u0435 \u0442\u043e\u0433\u043e, Kotlin-\u043a\u043e\u0440\u0443\u0442\u0438\u043d\u044b \u043e\u0431\u044f\u0437\u044b\u0432\u0430\u044e\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Kotlin, \u0434\u043b\u044f Loom-\u043a\u043e\u0440\u0443\u0442\u0438\u043d \u0436\u0435 \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Java, Scala, Kotlin, Groovy \u0438\u043b\u0438 \u043b\u044e\u0431\u043e\u0439 \u0434\u0440\u0443\u0433\u043e\u0439 JVM-\u044f\u0437\u044b\u043a.<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/785334\/\"> https:\/\/habr.com\/ru\/articles\/785334\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-364868","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/364868","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=364868"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/364868\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=364868"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=364868"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=364868"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}