1. 使用虚拟线程时谨慎使用 ThreadLocal,虚拟线程往往是大数量、短周期的,

  2. 使用虚拟线程时应避免使用 synchronized 锁,推荐使用 ReentrantLock

  3. 使用虚拟线程时避免直接调用本地方法

  4. 虚拟线程适用于 IO 密集型任务,不适用于 CPU 密集型任务

  5. Virtual threads are not faster threads; they do not run code any faster than platform threads. They exist to provide scale (higher throughput), not speed (lower latency).

  6. 虚拟线程可以即用即弃,永远不要池化管理

  7. ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); 是一个虚拟线程工厂,并不是虚拟线程池的意思,可以随意创建,推荐使用 try-with-resources

  8. The operating system schedules when a platform thread is run. However, the Java runtime schedules when a virtual thread is run.

  9. When the Java runtime schedules a virtual thread, it assigns or mounts the virtual thread on a platform thread, then the operating system schedules that platform thread as usual.

  10. 承载虚拟线程的平台线程也可以叫载体线程

  11. As a rule of thumb, if your application never has 10,000 virtual threads or more, it is unlikely to benefit from virtual threads. Either it experiences too light a load to need better throughput, or you have not represented sufficiently many tasks to virtual threads. 一般来说,如果你的应用程序永远不会拥有 10,000 个或更多的虚拟线程,那么它不太可能从虚拟线程中获益。要么是因为负载太轻,不需要更高的吞吐量,要么是因为你没有足够多地将任务分配给虚拟线程。

  12. 线程池的首要目的是共享稀缺资源——线程,与此同时还可以用线程池来限制并发(固定核心线程数和最大线程数),但是虚拟线程不需要被池化管理,因此可以使用信号量来限制并发。一个最简单的信号量使用示例:

    Semaphore sem = new Semaphore(10);
    ...
    Result foo() {
        sem.acquire();
        try {
            return callLimitedService();
        } finally {
            sem.release();
        }
    }