Skip to content

Instantly share code, notes, and snippets.

@srirajk
Created June 4, 2024 11:29
Show Gist options
  • Save srirajk/a2123efce097917bb9b8ccef0d4e78fa to your computer and use it in GitHub Desktop.
Save srirajk/a2123efce097917bb9b8ccef0d4e78fa to your computer and use it in GitHub Desktop.
platform vs virtual threads

OS Threads

  1. High Resource Use: Creating an OS thread requires significant memory and CPU resources, including a large stack size for each thread.
  2. Expensive Context Switching: Switching between threads involves saving and loading thread states, which is computationally expensive and slow.
  3. Kernel Mode Transitions: OS threads need to frequently transition between user and kernel modes, adding overhead to thread operations.
  4. Scheduling Overhead: The OS has to manage and schedule all threads, and as the number of threads grows, this overhead increases.
  5. Complex Synchronization: Managing access to shared resources between multiple threads requires complex synchronization, adding further overhead and potential for inefficiencies.

Virtual Threads

  1. Lightweight: Virtual threads are much lighter than OS threads, allowing for the creation of thousands of threads with minimal memory and CPU overhead.
  2. Efficient Task Management: They make it easy to handle many tasks concurrently, improving application performance and scalability.
  3. Simpler Code: Developers can write straightforward, synchronous code without worrying about complex thread management.
  4. Low Resource Usage: Virtual threads use fewer system resources, reducing the cost of context switching and memory consumption.
  5. Parking Feature: Virtual threads can be parked (paused) and unparked (resumed) efficiently without consuming OS resources, allowing them to wait for tasks like I/O operations without blocking system threads.
  6. Integration with Fork/Join: Virtual threads integrate well with the fork/join framework, enabling efficient parallel processing and workload distribution, further enhancing performance in concurrent applications.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment