Networks and Operating Systems - Process Management: Threads

CS2005 Networks and Operating Systems

  • Process management: Threads

  • Authors: A. Silberschatz, P. Baer Galvin and G. Gagne ©2003

Session Objectives

  • Introduce the notion of a thread as a fundamental unit of CPU utilization.

  • Discuss APIs for the Java thread library.

Key Questions

  • What is the difference between concurrency and parallelism?

  • What are threads, how are they used, and what are their benefits?

  • What is the lifecycle of a thread in Java?

Execution Models

Sequential Execution

  • Only one task or subtask (job, process) runs at a time.

  • Must complete before another task starts.

Concurrent Execution

  • Multiple tasks appear to run simultaneously.

  • Utilizes CPU time-slicing.

  • Involves process states where one task runs and another waits.

Parallel Execution

  • Multiple tasks run simultaneously on multiple CPUs/cores.

  • Requires hardware support for true parallelism.

Examples of Execution

Sequential Execution Example

  • Tasks executed one after the other, e.g., Task 1 runs then Task 2.

Concurrent Execution Example

  • Tasks interleave execution, e.g., Task 1 might run, then Task 2 when Task 1 is waiting.

Parallel Execution Example

  • Tasks executed simultaneously on different CPUs, e.g., Task 1 and Task 2 running at the same time.

Parallel Concurrent Execution

  • Combination of parallel and concurrent execution methods.

Concurrency vs. Parallelism

Definitions

  • Concurrency: Tasks start, run, and complete in overlapping time.

  • Parallelism: Tasks run at the same time on multiple CPUs/cores.

Key Points

  • Applications can be neither parallel nor concurrent (e.g., sequential processing).

  • Applications can also be parallel but not concurrent or concurrent but not parallel.

What is a Thread?

  • A thread is the smallest unit of CPU utilization.

  • Consists of: thread ID, program counter, register set, and stack.

  • Shares code section, data section, and resources with other threads in the same process.

  • Examples include web browsers and word processors that handle various tasks in separate threads.

Single-threaded vs. Multithreaded Process

  • Single-threaded: Only one thread of execution.

  • Multithreaded: Multiple threads allow concurrent execution within the same memory space.

Benefits of Threads

  • Responsiveness: Multithreading allows programs to remain responsive even when part of them is blocked.

  • Resource Sharing: Threads share the memory and resources of their parent process.

  • Economy: Thread creation is more resource-efficient than process creation.

  • Scalability: Multithreading enhances performance in multiprocessor architectures by executing threads in parallel.

Multithreaded Server Architecture

  • A single-threaded server handles one client request at a time.

  • Multithreaded servers create new threads for each client request, improving concurrency.

Extra: Multicore Programming

  • Development evolution led to systems with multiple CPU cores.

  • Multithreaded programming enhances concurrency and performance in multicore systems.

Speedup – Amdahl’s Law

  • Describes potential performance gains from adding cores to applications with both serial and parallel components.

  • Formula: speedup ≤ 1 / (S + (1 - S) / N)(Where S is the serial portion and N is the number of cores.)

Challenges in Multicore Programming

  • Identifying tasks: Applications must be divided into independent, concurrent tasks.

  • Load balancing: Ensuring tasks are evenly distributed across processors.

  • Data splitting: Managing data dependencies among tasks to prevent errors.

  • Testing and debugging: Complexity increases significantly with parallel execution.

Thread Programming in Java

  • Ways to create threads:

    1. By extending the Thread class.

    2. By implementing the Runnable interface.

Java Threads Lifecycle

  • States: NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED.

  • Transitions between states depend on thread actions and other threads' statuses.

Key States

  • NEW: Thread created but not started.

  • RUNNABLE: Active in execution.

  • BLOCKED: Waiting for a monitor lock.

  • WAITING: Waiting indefinitely for a specific action.

  • TIMED_WAITING: Waiting with a specified timeout.

  • TERMINATED: Thread completed or terminated.

Recap

  • Multithreading maximizes CPU utilization through simultaneous execution of threads, sharing of memory, and lightweight processes.

Creating (Invoking) a Thread

  • Two methods to create threads:

    1. Extending the Thread class (with built-in thread functionality).

    2. Implementing the Runnable interface (allowing class extensions).

Extending the java.lang.Thread Class

  • Override run() method in the class extending Thread.

  • Use the start() method to begin thread execution.

Java Threads States

  • Understand states like NEW, RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, TERMINATED to manage threads efficiently.

Session Objectives (Recap)

  • Understanding threads' role, benefits, and lifecycle within Java's concurrency model is crucial for effective programming.