Virtual Thread not perform right when use with Disruptor workPool in openjdk19

Ron Pressler ron.pressler at oracle.com
Thu Aug 4 08:12:03 UTC 2022


Hi.

1. Virtual threads should *never* be pooled. There can be no benefit to replacing a platform thread with a virtual thread in a pool, because the difference between virtual threads and platform threads is that you can have lots and lots of virtual threads.

The benefit from virtual threads comes when abandoning thread pools altogether and spawning a new thread for each request in your system. I.e. you do not replace platform threads with virtual threads; you replace requests with virtual threads. If you find yourself pooling virtual threads, you’ve made a wrong turn somewhere.

2. I’m not 100% sure what the test does, but the name “YieldingWaitStrategy” hints that your threads don't block. Virtual threads exist to make blocking cheap. If they don’t block, then their scheduler — which is designed with blocking work in mind — might exhaust all of its resources and not be get around to running that last thread you create.

— Ron

On 4 Aug 2022, at 08:58, 地上的月影 <404450559 at qq.com<mailto:404450559 at qq.com>> wrote:

Hi All
    I'm one of openjdk user and these days I tried to use the preview feature Virtual Thread with Disruptor. when use ThreadFactory in Disruptor workerPool, somingthing seems wrong that when I used Thread.ofVirtual().start. the new created virtualThread seems don't start? it don't print hello world, I'm not sure whether it's a bug or I use it wrong, here are my core test code

    public void test2() throws InterruptedException {
        System.out.println( "Start Test!" );
        RingBuffer<Order> ringBuffer = RingBuffer.create(ProducerType.MULTI, new EventFactory<Order>() {
            @Override
            public Order newInstance() {
                return new Order();
            }
        }, 1024 * 1024, new YieldingWaitStrategy());
        SequenceBarrier barrier = ringBuffer.newBarrier();
        Consumer[] consumers=  new Consumer[10];
        for (int i = 0; i < consumers.length; i++) {
            consumers[i] = new Consumer("C"+i);
        }

        WorkerPool<Order> workerPool = new WorkerPool<>(ringBuffer, barrier, new EventExceptionHandle(), consumers);
        ringBuffer.addGatingSequences(workerPool.getWorkerSequences());
        ThreadFactory factory_v = Thread.ofVirtual().factory();
        ExecutorService pool = Executors.newFixedThreadPool(10, factory_v);
        workerPool.start(pool);

        // don't print hello world?
        Thread thd = Thread.ofVirtual().start(() -> System.out.println("Hello world!"));
        Thread.sleep(5000);
        System.out.println("End Test!" );
    }

looking forward to your answers.
best wishes!
-Anjian Wen



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/loom-dev/attachments/20220804/7ae64391/attachment-0001.htm>


More information about the loom-dev mailing list