• Home
    • View
    • Login
    This page
    • Normal
    • Export PDF
    • Page Information

    Loading...
  1. Dashboard
  2. Undefined Space
  3. Loom
  4. Getting started

Getting started

Skip to end of metadata
  • Created by Alan Bateman, last modified on Apr 21, 2020
Go to start of metadata

Thread API

The following use a static factory method to create a virtual thread, invokes its start method to schedule it, and then invokes the join method to wait for thread to terminate.

    Thread thread = Thread.newThread(Thread.VIRTUAL, () -> System.out.println("hello"));
thread.start();
thread.join();


The Thread.Builder API can also be used to creates virtual thread. The first example creates a virtual thread but does not start it. The second example creates and start the virtual thread.

   Thread thread1 = Thread.builder().virtual().task(() -> System.out.println("hello")).build();

   Thread thread2 = Thread.builder().virtual().task(() -> System.out.println("hi")).start(); 


The Thread.Builder API can also be used to create a ThreadFactory. The ThreadFactory created by the following snippet will create virtual threads named "worker-0", "worker-1", "worker-2", ...

    ThreadFactory factory = Thread.builder().virtual().name("worker", 0).factory();


ExecutorService API

TDB



Differences between regular Threads and virtual Threads

Thread API

  1. The priority of a virtual thread is always NORM_PRIORITY. The priority is not inherited and cannot be changed with the setPriority method.
  2. Virtual threads are daemon threads and cannot be changed with the setDaemon method.
  3. Virtual threads cannot be suspend, resumed or stopped with the Thread suspend, resume and stop APIs.
  4. Virtual threads have no permission when running with a security manager.
  5. Virtual threads are not active threads in their thread group. The getThreadGroup method returns a ThreadGroup that cannot be destroyed and its enumerate methods do not enumerate the virtual threads in the group.

Networking APIs

If a virtual thread is interrupted when blocked in an I/O operating on a java.net.Socket, ServerSocket or DatagramSocket then it causes IOException to be thrown. 


Overview
Content Tools
ThemeBuilder
  • No labels

Terms of Use • License: GPLv2 • Privacy • Trademarks • Contact Us

Powered by a free Atlassian Confluence Open Source Project License granted to https://www.atlassian.com/software/views/opensource-community-additional-license-offer. Evaluate Confluence today.

  • Adaptavist ThemeBuilder Powered by Atlassian Confluence 7.4.1
  • Adaptavist ThemeBuilder printed.by.atlassian.confluence
  • Report a bug
  • Atlassian News
Atlassian
Adaptavist ThemeBuilder EngineAtlassian Confluence
{"serverDuration": 166, "requestCorrelationId": "e8b9622b46d1cab2"}