Page cover image

Introduction

Why MultiThreading?

  • Better CPU Utilization

  • Better IO Utilization

  • Higher application responses

Issues with Multi-Threading

Shared Mutable State issues:

  • Race Conditions

  • Invisible Writes

  • Congestion

  • DeadLock

  • Nested Monitor Lockout

  • Starvation

  • Slipped Condition

  • Missed Signals

No Shared Mutable state concurrency:

  • Separate State Concurrency

  • Functional Paralleism

  • Parallel Pipelines

Multi-Threading Costs:

  • More Complex Design - Code executed by multiple threads accessing shared data need special attention. Thread interaction is far from always simple. Errors arising from incorrect thread synchronization can be very hard to detect, reproduce and fix.

  • Context Switching Overhead - When a CPU switches from executing one thread to executing another, the CPU needs to save the local data, program pointer, etc. of the current thread, and load the local data, program pointer, etc. of the next thread to execute. This switch is called a "context switch". The CPU switches from executing in the context of one thread to executing in the context of another.

  • Increased resource consumption - Threads needs some memory to maintain its local stack it consumes more resources inside OS.

Appendix:

TypeUse Case

Volatile

Flags

AtomicInteger AtomicLong

Counters

AtomicReference

  1. Caches (Building new cache in the background and replacing atomically)

  2. Used By some internal classes

  3. Non Blocking Algorithms

Last updated