No internet connection
  1. Home

Comments for https://horstmann.com/unblog/2022-04-15/index.html

By System @system
    2022-06-21 15:25:58.392Z
    • 11 comments
    1. C
      Clever Code @clever_code
        2022-06-21 15:25:58.458Z2022-06-21 17:37:27.833Z

        Thanks for article.

        Tomcat 10.1.0-M16 (beta) Released

        The notable changes in this release are:

        Refactor synchronization blocks locking on SocketWrapper to use ReentrantLock to support users wishing to experiment with project Loom.

        Almost same code yours in SocketProcessorBase.java

        1. K
          In reply tosystem:
          Kien Vo @KienVo
            2022-11-21 02:40:37.106Z

            Hi Horstmann,

            I tested the virtual threads in my local machine comparing with "old" platform threads and the result surprised me that virtual threads are much worse (multiple times) than platform threads.
            Below is my test function (the result is the same for both local or remote API). Could you give any advice as I'm quite confused now?

            static void handleUrlVirtualThreads() {
                int nThreads = 10_000;
            //    try (var executor = Executors.newFixedThreadPool(100)) {
                try (var executor = Executors.newVirtualThreadPerTaskExecutor()) {
                  IntStream.range(0, nThreads).forEach(i -> executor.submit(() -> {
                    try {
                      var url = new URL("http://localhost:8090/hello"); //http://asia-southeast1.google.api.expert/ip
                      try (var in = url.openStream()) {
                        return new String(in.readAllBytes(), StandardCharsets.UTF_8);
                      }
                    } catch (IOException e) {
                      throw new RuntimeException(e);
                    }
                  }));
                }
              }
            
            1. TTheodore Ravindranath @TheodoreRavi
                2023-10-12 04:36:33.339Z

                Did you shutdown the Executor and awaitTermination? Otherwise, it just creates the tasks and leaves them running and moves out of that method handleUrlVirtualThreads.
                Also, if the response of the 3rd party site is very fast, then you may not see much benefit as 100 threads can process 10_000 calls easily.

                More importantly, I think the benefit of virtual threads comes in when there are assorted tasks with different work profile.
                For example, imagine 100 threads are blocked with calls to a very slow website. Now, imagine you get 50 more tasks which just need to access files on harddisk.
                In the case of executor, only when the threads start to become unblocked, executor can start working on the other 50 "easy" tasks which just need local file access.
                However, with virtual threads, all 150 operations will be started simultaneously, and the 50 fast ones will be completed almost immediately.

                1. CCay Horstmann @cayhorstmann
                    2023-10-12 09:25:30.313Z

                    Actually, the executor is shut down in the try-with-resources statement. It's easy to get blacklisted when sending 10,000 requests in quick succession, so I tried it with 1,000 with the http://asia-southeast1.google.api.expert/ip service. Virtual threads: 3 seconds, platform threads: 7 seconds.

                2. T
                  In reply tosystem:
                  tambda @tambda
                    2022-08-16 07:26:59.929Z

                    I will download code.zip,just 404

                    1. CCay Horstmann @cayhorstmann
                        2022-08-16 17:41:50.404Z

                        Try it again--I just refreshed it.

                      • O
                        In reply tosystem:
                        Oliver @oliver
                          2023-04-25 10:27:51.971Z

                          Looks like this change is also included in Tomcat 9.1.74 now: "Refactor synchronization blocks locking on SocketWrapper to use ReentrantLock to support users wishing to experiment with project Loom."

                          1. T
                            In reply tosystem:
                            Theodore Ravindranath @TheodoreRavi
                              2023-10-12 05:34:39.564Z

                              Very useful article.. thanks so much.

                              As I mentioned in my other comment, I feel being non-blocking really shines when there are lots of long-waiting tasks mixed with lots of small tasks.
                              The blocking API / thread pools will wait for some of the long-waiting tasks to complete before taking up the small tasks. Whereas non-blocking will simply go through with the smaller tasks.
                              I believe that is the key benefit of Virtual threads.

                              1. CCay Horstmann @cayhorstmann
                                  2023-10-12 09:28:51.957Z

                                  The sweet spot of virtual threads is lots of tasks that mostly block. The key advantage of virtual threads is to program in a natural Java style, with branches, loops, method calls, and exception handling, instead of a reactive style with callbacks. Don't use virtual threads for non-blocking tasks.

                                  1. CCay Horstmann @cayhorstmann
                                      2023-10-13 05:26:08.838Z

                                      Absolutely. That's why the creators of virtual threads stress that they increase throughput. It's not the only way to increase throughput. A reactive architecture will do the same. But at the cost of an unnatural programming model.

                                      1. In reply tocayhorstmann:
                                        TTheodore Ravindranath @TheodoreRavi
                                          2023-10-13 04:37:13.559Z

                                          "The sweet spot of virtual threads is lots of tasks that mostly block" - while I agree with this, isn't it also true that they let other tasks through while native threads would really be blocked.
                                          Imagine lot of web controller requests, doing assorted operations, if 200 native threads were blocked on 50 DB connections, all 200 threads would be stuck, whereas virtual threads would allow other customer requests which don't involve the DB to go through. That's why virtual threads really shine when there is an assorted work load. The point is, there are more requests which are not waiting for the same 50 DB connections, which will go through in case of Virtual threads.
                                          And yes, we don't have to do non-blocking and reactive to achieve this - in case of virtual threads, we can work with the traditional/convenient iterative model.