Introduction

Motivating factors led to the development of operating systems -

  • Resource utilization : Programs sometimes have to wait
  • Fairness : time slicing
  • Convenience : program coordination

Threads allow multiple streams of program control flow to coexist within a process.

They share process-wide resources such as memory and file handles, but each thread has its own program counter, stack, and local variables

multiple threads within the same program can be scheduled simultaneously on multiple CPUs

Benefits of threads

  • improve the performance of complex applications
  • reduce development and maintenance costs

Exploiting multiple processors

  • When properly designed, multithreaded programs can improve throughput by utilizing available processor resources more effectively.

Risks of threads

  • Safety hazards
  • Liveness hazards

    • use of threads introduces additional forms of liveness failure that do not occur in single-threaded programs.
    • A liveness failure occurs when an activity gets into a state such that it is permanently unable to make forward progress
  • Performance hazards

    • Context switches—when the scheduler suspends the active thread temporarily so another thread can run—are more frequent in applications with many threads, and have significant
    • costs: saving and restoring execution context, loss of locality, and CPU time spent scheduling threads instead of running them.

Threads are everywhere

  • Every Java application uses threads.
  • When the JVM starts, it creates threads for JVM housekeeping tasks (garbage collection,finalization) and a main thread for running the main method.

results matching ""

    No results matching ""