Project Loom presentation at BangaloreJUG

Alan Bateman Alan.Bateman at oracle.com
Fri Dec 20 12:41:24 UTC 2019


On 20/12/2019 12:27, Nawazish Khan wrote:
> Can anybody help me coming up with a contrived example of running a 
> producer-consumer pair in their own virtual threads, but on a _single 
> threaded_ executor (schedular). Thanks in advance.
This might be close to what you are looking for with the current prototype:

         try (ExecutorService scheduler = Executors.newFixedThreadPool(1)) {
             ThreadFactory factory = 
Thread.builder().virtual(scheduler).factory();
             try (ExecutorService executor = 
Executors.newUnboundedExecutor(factory)) {
                 executor.submit(() -> produce());
                 executor.submit(() -> consume());
             }
         }

This is one carrier thread and two virtual threads. Butmaybe you are 
looking for an example using channels or queues rather than the setup to 
run the threads? This is a topic that we hope to make progress on in the 
new year. The existing blocking queues in java.util.concurrent will work 
of course but they aren't tuned/suitable for virtual threads at this time.

-Alan





More information about the loom-dev mailing list