{"id":341942,"date":"2022-11-30T15:00:18","date_gmt":"2022-11-30T15:00:18","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=341942"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=341942","title":{"rendered":"<span>\u041d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0432\u0442\u043e\u0440 (retry) \u0432 Java \u0438 \u043f\u0440\u043e\u0435\u043a\u0442 Loom<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h1 id=\"neblokiruyuschiy-povtor-retry-v-java-i-proekt-loom\">\u041d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0432\u0442\u043e\u0440 (retry) \u0432 Java \u0438 \u043f\u0440\u043e\u0435\u043a\u0442 Loom<\/h1>\n<p>  <\/p>\n<h2 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440 (retry) \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0440\u0435\u0439\u0448\u0438\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0433\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f. \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044b \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 HTTP \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0442\u0441\u044b\u043b\u043a\u0435 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b \u0438 \u043f\u0440\u043e\u0447. \u0438 \u043f\u0440\u043e\u0447.<\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"naivnyy-povtor\">\u041d\u0430\u0438\u0432\u043d\u044b\u0439 \u043f\u043e\u0432\u0442\u043e\u0440<\/h2>\n<p>  <\/p>\n<p>\u0415\u0441\u043b\u0438 \u0412\u044b \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043b\u0438, \u0442\u043e \u043d\u0430\u0432\u0435\u0440\u043d\u044f\u043a\u0430 \u043f\u0438\u0441\u0430\u043b\u0438 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u043a\u0430\u043a\u043e\u0433\u043e \u043b\u0438\u0431\u043e \u043f\u043e\u0432\u0442\u043e\u0440\u0430. \u041f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u0439 \u043f\u043e\u0432\u0442\u043e\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u0446\u0438\u043a\u043b \u0441 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u043c \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435\u043c \u043f\u043e\u0441\u043b\u0435 \u043d\u0435\u0443\u0434\u0430\u0447\u043d\u043e\u0439 \u043f\u043e\u043f\u044b\u0442\u043a\u0438. \u041f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432\u043e\u0442 \u0442\u0430\u043a.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    static &lt;T> T retry(long maxTries, Duration delay, CheckedSupplier&lt;T> supp) {         for (int i = 0; i &lt; maxTries; i++) {             try {                 return supp.get();             } catch (Exception e) {                 if (i &lt; maxTries - 1) { \/\/not last attempt                     try {                         Thread.sleep(delay.toMillis());                     } catch (InterruptedException ie) {                         Thread.currentThread().interrupt(); \/\/Propagate interruption                          break;                     }                 }             }         }         throw new RuntimeException(\"Retry failed after %d retries\".formatted(maxTries));      }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u043e\u0432\u0442\u043e\u0440\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u044f \u043a \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445. \u041c\u044b \u0434\u0435\u043b\u0430\u0435\u043c 10 \u043f\u043e\u043f\u044b\u0442\u043e\u043a \u0441 \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u043e\u0439 100 msec.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    Connection retryConnection() {         return retry(10, Duration.ofMillis(100), () -> DriverManager.getConnection(\"jdbc:foo\"));     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u042d\u0442\u043e\u0442 \u043a\u043e\u0434 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0412\u0430\u0448 \u043f\u043e\u0442\u043e\u043a \u043d\u0430 \u043e\u0434\u043d\u0443 \u0441\u0435\u043a\u0443\u043d\u0434\u0443 (\u0442\u043e\u0447\u043d\u0435\u0435 900 milliseconds, \u043c\u044b \u043d\u0435 \u0436\u0434\u0435\u043c \u043f\u043e\u0441\u043b\u0435 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u043f\u043e\u043f\u044b\u0442\u043a\u0438) \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e <code>Thread.sleep()<\/code> \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0435\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439. \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0446\u0435\u043d\u0438\u0442\u044c \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043c\u0435\u0442\u043e\u0434\u0430 \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u0445 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (threads) \u0438 \u0432\u0440\u0435\u043c\u0435\u043d\u0438. \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c \u043d\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c 12 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e\u0432\u0442\u043e\u0440\u0430. \u041d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f 12 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0447\u0442\u043e\u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043d\u043e\u0435 \u0437\u0430 \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0432\u0440\u0435\u043c\u044f 1 \u0441\u0435\u043a, 6 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0442 \u043f\u043e\u0432\u0442\u043e\u0440\u044b \u043d\u0430 2 \u0441\u0435\u043a, 4 \u2014 \u0437\u0430 \u0442\u0440\u0438 \u0441\u0435\u043a\u0443\u043d\u0434\u044b, \u043e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a \u2014 \u0437\u0430 12 \u0441\u0435\u043a\u0443\u043d\u0434. \u0410 \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u043d\u0430\u043c \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c 1000 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043f\u043e\u0432\u0442\u043e\u0440\u0430? \u0414\u043b\u044f \u0431\u044b\u0441\u0442\u0440\u043e\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f 1000 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 (\u043d\u0435\u0442, \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0435 \u044d\u0442\u043e) \u0438\u043b\u0438 16 \u043c\u0438\u043d\u0443\u0442 \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u043f\u043b\u043e\u0445\u043e \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f.<\/p>\n<p>  <\/p>\n<p>\u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u043c \u044d\u0442\u043e \u043d\u0430 \u0442\u0435\u0441\u0442\u0435<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    public void testNaiveRetry() throws Exception {         ExecutorService ex = Executors.newFixedThreadPool(4);       \/\/4 threads         Duration dur = TryUtils.measure(() -> {             IntStream.range(0, 12)                                  \/\/12 retries             .mapToObj(i -> ex.submit(() -> this.retryConnection())) \/\/submit retry             .toList()                                               \/\/collect all              .forEach(f -> Try.of(()->f.get()));                     \/\/wait all finished         });         System.out.println(dur);                                         }  Output: PT2.723379388S<\/code><\/pre>\n<p>  <\/p>\n<p>\u0422\u0435\u043e\u0440\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438: 900 ms * 3 = 2.7 sec, \u0445\u043e\u0440\u043e\u0448\u0435\u0435 \u0441\u043e\u0432\u043f\u0430\u0434\u0435\u043d\u0438\u0435<\/p>\n<p>  <\/p>\n<h2 id=\"neblokiruyuschiy-povtor\">\u041d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0432\u0442\u043e\u0440<\/h2>\n<p>  <\/p>\n<p>\u0410 \u043c\u043e\u0436\u043d\u043e \u043b\u0438 \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044b \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044f \u043f\u043e\u0442\u043e\u043a\u0438? \u041c\u043e\u0436\u043d\u043e, \u0435\u0441\u043b\u0438 \u0432\u043c\u0435\u0441\u0442\u043e <code>Thread.sleep()<\/code> \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043f\u0435\u0440\u0435\u043f\u043b\u0430\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435 \u043f\u043e\u0442\u043e\u043a\u0430 \u043d\u0430 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>CompletableFuture.delayedExecutor()<\/code>. \u041a\u0430\u043a \u044d\u0442\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043c\u043e\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0435 <code>Retry.java<\/code>. \u041a\u0441\u0442\u0430\u0442\u0438 \u043f\u043e\u0434\u043e\u0431\u043d\u044b\u0439 \u043f\u043e\u0434\u0445\u043e\u0434 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0432 \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0435\u043c \u043c\u0435\u0442\u043e\u0434\u0435 <code>delay()<\/code> \u0432 Kotlin.<\/p>\n<p>  <\/p>\n<p>Retry.java \u2014 \u043a\u043e\u043c\u043f\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441 \u0431\u0435\u0437 \u0432\u043d\u0435\u0448\u043d\u0438\u0445 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0435\u0439 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u043e\u0436\u0435\u0442 \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u043f\u043e\u0432\u0442\u043e\u0440 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 ( <a href=\"https:\/\/github.com\/skopylov58\/java-functional-addons\/blob\/master\/function\/src\/main\/java\/com\/github\/skopylov58\/retry\/Retry.java\" rel=\"nofollow noopener noreferrer\">\u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0439 \u043a\u043e\u0434<\/a>, <a href=\"https:\/\/skopylov58.github.io\/java-functional-addons\/com\/github\/skopylov58\/retry\/package-summary.html\" rel=\"nofollow noopener noreferrer\">Javadoc<\/a> ).<br \/>  \u041f\u043e\u043b\u043d\u043e\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0435\u0439 <code>Retry<\/code> <a href=\"https:\/\/github.com\/skopylov58\/java-functional-addons#retryt---non-blocking-asynchronous-functional-retry-procedure\" rel=\"nofollow noopener noreferrer\">\u0441 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0442\u0443\u0442<\/a> <\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043c\u044b \u0443\u0436\u0435 \u0434\u0435\u043b\u0430\u043b\u0438 \u0432\u044b\u0448\u0435.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    CompletableFuture&lt;Connection> retryConnection(Executor ex) {         return Retry.of(() -> DriverManager.getConnection(\"jdbc:foo\"))             .withFixedDelay(Duration.ofMillis(100))             .withExecutor(ex)             .retry(10);     }<\/code><\/pre>\n<p>  <\/p>\n<p>\u041c\u044b \u0432\u0438\u0434\u0438\u043c \u0447\u0442\u043e \u0437\u0434\u0435\u0441\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442\u0441\u044f \u043d\u0435 <code>Connection<\/code>, \u0430 <code>CompletableFuture&lt;Connection><\/code>, \u0447\u0442\u043e \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u043e\u0431 \u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u0440\u0438\u0440\u043e\u0434\u0435 \u044d\u0442\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c 1000 \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 <code>Retry<\/code>.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    public void testRetry() throws Exception {         Executor ex = Executors.newSingleThreadExecutor(); \/\/\u041e\u0434\u0438\u043d \u043f\u043e\u0442\u043e\u043a         Duration dur = TryUtils.measure(() -> {      \/\/\u042d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u0430\u044f \u0443\u0442\u0438\u043b\u0438\u0442\u0430 \u0434\u043b\u044f \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0432\u0440\u0435\u043c\u0435\u043d             IntStream.range(0, 1_000)                \/\/1000 \u0440\u0430\u0437             .mapToObj(i -> this.retryConnection(ex)) \/\/\u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044e \u043f\u043e\u0432\u0442\u043e\u0440\u0430             .toList()                                \/\/\u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c future \u0432 \u0441\u043f\u0438\u0441\u043e\u043a             .forEach(f -> Try.of(()->f.get()));      \/\/\u0434\u043e\u0436\u0438\u0434\u0430\u0435\u043c\u0441\u044f \u0432\u0441\u0435\u0445 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432         });         System.out.println(dur);                     \/\/\u043f\u0435\u0447\u0430\u0442\u0430\u0435\u043c \u043f\u0440\u043e\u0448\u0435\u0434\u0448\u0435\u0435 \u0432\u0440\u0435\u043c\u044f     }  Output: PT1.065544748S    <\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u043c\u044b \u0432\u0438\u0434\u0438\u043c, <code>Retry<\/code> \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 1000 \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432 \u0432 \u043e\u0434\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435 \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0435\u043a\u0443\u043d\u0434\u044b. \u0423\u0440\u0430, \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043f\u043e\u0432\u0442\u043e\u0440\u044b.<\/p>\n<p>  <\/p>\n<h2 id=\"prichem-zdes-proekt-loom\">\u041f\u0440\u0438\u0447\u0435\u043c \u0437\u0434\u0435\u0441\u044c \u043f\u0440\u043e\u0435\u043a\u0442 Loom?<\/h2>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 Loom \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u0432 JDK 19 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 (\u043f\u043e\u043a\u0430 \u0432 \u0441\u0442\u0430\u0434\u0438\u0438 preview). \u0426\u0435\u043b\u044c \u0432\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432 \u043b\u0443\u0447\u0448\u0435 \u0432\u0441\u0435\u0433\u043e \u043e\u043f\u0438\u0441\u0430\u043d\u0430 \u0432 <a href=\"https:\/\/openjdk.org\/jeps\/425\" rel=\"nofollow noopener noreferrer\">JEP 425<\/a> \u0438 \u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u044e \u044d\u0442\u043e \u043a \u043f\u0440\u043e\u0447\u0442\u0435\u043d\u0438\u044e.<\/p>\n<p>  <\/p>\n<p>\u0412\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044f\u0441\u044c \u043a \u043d\u0430\u0448\u0435\u0439 \u0442\u0435\u043c\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u0430 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439, \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u0441\u043a\u0430\u0436\u0443 \u0447\u0442\u043e <code>Thread.sleep()<\/code> \u0431\u043e\u043b\u0435\u0435 \u043d\u0435 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0435\u0439 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0435\u0439 \u0431\u0443\u0434\u0443\u0447\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c\u043e\u0439 \u0432 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435. \u0422\u043e\u0447\u043d\u0435\u0435 \u0433\u043e\u0432\u043e\u0440\u044f, <code>sleep()<\/code> \u043f\u0440\u0438\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442 (suspend) \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a, \u0434\u0430\u0432\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u043e\u043c\u0443 \u043f\u043e\u0442\u043e\u043a\u0443 (carrier thread) \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0438\u0442\u044c\u0441\u044f \u043d\u0430 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u043e\u0432. \u041f\u043e\u0441\u043b\u0435 \u0438\u0441\u0442\u0435\u0447\u0435\u043d\u0438\u044f \u0441\u0440\u043e\u043a\u0430 \u0441\u043d\u0430, \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0432\u043e\u0437\u043e\u0431\u043d\u043e\u0432\u0438\u0442 (resume) \u0441\u0432\u043e\u044e \u0440\u0430\u0431\u043e\u0442\u0443. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u0438\u0432\u043d\u044b\u0439 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u0430 \u0432 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u043e\u0442\u043e\u043a\u0430\u0445.<\/p>\n<p>  <\/p>\n<pre><code class=\"java\">    var dur =TryUtils.measure(() -> {         ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor();         IntStream.range(0, 1_000)         .mapToObj(i -> executor.submit(() -> retryConnection()))         .toList()         .forEach(f -> Try.of(() -> f.get()));     });     System.out.println(dur);  Output: PT1.010342284S<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0438\u043c\u0435\u0435\u043c \u0447\u0443\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0439 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u043d\u0430 1000 \u043f\u043e\u0432\u0442\u043e\u0440\u043e\u0432, \u043a\u0430\u043a \u0438 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 <code>Retry<\/code>.<\/p>\n<p>  <\/p>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 Loom \u043f\u0440\u0438\u043d\u0435\u0441\u0435\u0442 \u043a\u0430\u0440\u0434\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u0443 Java.<\/p>\n<p>  <\/p>\n<ul>\n<li>\n<p>\u0421\u0442\u0438\u043b\u044c \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u043d\u0430 \u0437\u0430\u043f\u0440\u043e\u0441 (thread per request) \u043c\u0430\u0441\u0448\u0442\u0430\u0431\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441 \u043f\u043e\u0447\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0435\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u043e\u0440\u043d\u044b\u0445 \u044f\u0434\u0435\u0440. \u042d\u0442\u043e\u0442 \u0441\u0442\u0438\u043b\u044c \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0443\u0435\u043c\u044b\u043c.<\/p>\n<p>  <\/li>\n<li>\n<p>\u0412\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u043d\u0435 \u0443\u0441\u043b\u043e\u0436\u043d\u044f\u044e\u0442 \u043c\u043e\u0434\u0435\u043b\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0438 \u043d\u0435 \u0442\u0440\u0435\u0431\u0443\u044e\u0442 \u0438\u0437\u0443\u0447\u0435\u043d\u0438\u044f \u043d\u043e\u0432\u044b\u0445 \u043a\u043e\u043d\u0446\u0435\u043f\u0446\u0438\u0439, \u0430\u0432\u0442\u043e\u0440 JEP 425 \u0433\u043e\u0432\u043e\u0440\u0438\u0442 \u0447\u0442\u043e \u0441\u043a\u043e\u0440\u0435\u0435 \u043d\u0443\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u0437\u0430\u0431\u044b\u0442\u044c \u0441\u0442\u0430\u0440\u044b\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043a\u0438 (&#171;it may require unlearning habits developed to cope with today&#8217;s high cost of threads.&#187;)<\/p>\n<p>  <\/li>\n<li>\n<p>\u041c\u043d\u043e\u0433\u0438\u0435 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u044b\u0435 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u044b \u0434\u043b\u044f \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u043f\u043e\u0442\u043e\u043a\u0430\u043c\u0438.\u0422\u0430\u043a \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u0447\u0442\u0435\u043d\u0438\u044f \u0438\u0437 \u0441\u043e\u043a\u0435\u0442\u0430 \u0441\u0442\u0430\u043d\u0443\u0442 \u043d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u043c\u0438 \u0432 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u043e\u0442\u043e\u043a\u0435.<\/p>\n<p>  <\/li>\n<li>\n<p>\u0420\u0435\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439\/\u0430\u0441\u0438\u043d\u0445\u0440\u043e\u043d\u043d\u044b\u0439 \u0441\u0442\u0438\u043b\u044c \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043d\u0435 \u043d\u0443\u0436\u043d\u044b\u043c.<\/p>\n<p>  <\/li>\n<\/ul>\n<p>  <\/p>\n<p>\u041d\u0430\u0441 \u0436\u0434\u0443\u0442 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0430 \u0432 \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0435\u043c \u0431\u0443\u0434\u0443\u0449\u0435\u043c. \u042f \u043e\u0447\u0435\u043d\u044c \u043d\u0430\u0434\u0435\u044e\u0441\u044c \u0447\u0442\u043e \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0435 \u043f\u043e\u0442\u043e\u043a\u0438 \u0441\u0442\u0430\u043d\u0443\u0442 \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0439 \u0447\u0430\u0441\u0442\u044c\u044e JDK \u0432 \u043e\u0447\u0435\u0440\u0435\u0434\u043d\u043e\u0439 LTS \u0432\u0435\u0440\u0441\u0438\u0438 Java. <\/p>\n<p>  <\/p>\n<p>\u0421\u0435\u0440\u0433\u0435\u0439 \u041a\u043e\u043f\u044b\u043b\u043e\u0432<br \/>  skopylov@gmail.com<br \/>  <a href=\"https:\/\/github.com\/skopylov58\/cv\" rel=\"nofollow noopener noreferrer\">\u0420\u0435\u0437\u044e\u043c\u0435<\/a><br \/>  <a href=\"https:\/\/github.com\/skopylov58\/\" rel=\"nofollow noopener noreferrer\">\u0421\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043a\u043e\u0434 \u043d\u0430 Github<\/a><\/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\/post\/702628\/\"> https:\/\/habr.com\/ru\/post\/702628\/<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h1 id=\"neblokiruyuschiy-povtor-retry-v-java-i-proekt-loom\">\u041d\u0435\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043f\u043e\u0432\u0442\u043e\u0440 (retry) \u0432 Java \u0438 \u043f\u0440\u043e\u0435\u043a\u0442 Loom<\/h1>\n<p>  <\/p>\n<h2 id=\"vvedenie\">\u0412\u0432\u0435\u0434\u0435\u043d\u0438\u0435<\/h2>\n<p>  <\/p>\n<p>\u041f\u043e\u0432\u0442\u043e\u0440 (retry) \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u0440\u0435\u0439\u0448\u0438\u043c \u043c\u0435\u0445\u0430\u043d\u0438\u0437\u043c\u043e\u043c \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f \u043d\u0430\u0434\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u043d\u043e\u0433\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u044f. \u041c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u043f\u043e\u0432\u0442\u043e\u0440\u044b \u043f\u0440\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 HTTP \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u043e\u0442\u0441\u044b\u043b\u043a\u0435 \u044d\u043b\u0435\u043a\u0442\u0440\u043e\u043d\u043d\u043e\u0439 \u043f\u043e\u0447\u0442\u044b \u0438 \u043f\u0440\u043e\u0447. \u0438 \u043f\u0440\u043e\u0447.<\/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-341942","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341942","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=341942"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/341942\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=341942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=341942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=341942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}