Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The following is an example that creates a virtual thread that puts an element into a queue when it has completed a task, the . The main thread blocks on the queue, waiting for the element.

Code Block
languagejava
        var queue = new SynchronousQueue<String>();

        Thread.startVirtualThread(() -> {
            try {
                Thread.sleep(Duration.ofSeconds(2));
                queue.put("done");
            } catch (InterruptedException e) { }

        });

        String msg = queue.take();

...