[go: up one dir, main page]

0% found this document useful (0 votes)
9 views15 pages

Process and Thread Management

The document discusses the inefficiencies of managing threads without pools and introduces thread pooling as a solution that improves performance and resource management by reusing threads. It outlines the advantages of multithreading, such as enhanced responsiveness and resource utilization, and details the implementation of thread pooling in Java using the Runnable and Executor interfaces. Additionally, it covers handling thread interruptions and custom exception handling for better control over thread management.

Uploaded by

Anh Duc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

Process and Thread Management

The document discusses the inefficiencies of managing threads without pools and introduces thread pooling as a solution that improves performance and resource management by reusing threads. It outlines the advantages of multithreading, such as enhanced responsiveness and resource utilization, and details the implementation of thread pooling in Java using the Runnable and Executor interfaces. Additionally, it covers handling thread interruptions and custom exception handling for better control over thread management.

Uploaded by

Anh Duc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Process and Thread

Management
Group 14: Vũ Thái Hưng 20226046

Nguyễn Bùi Đức Anh 20220070

Nguyễn Trọng Bách 20226014


Problem Description
Managing threads without pools is inefficient for
applications with many concurrent tasks. Constant
thread creation and destruction lead to overhead,
impacting performance and resource use.

—> Thread pooling addresses these issues by reusing


threads, thus improving performance and stability.
Threads

Definition
A thread is a basic CPU using unit. It consists of
ThreadID, Program Counter, Registers, and Stack
space. Multiple threads can share resources within a
process.

Advantages
Threads offer responsiveness, effective multiprocessor
utilization, resource sharing, enhanced communication,
increased throughput, and economic benefits.
Multi-threading
1 Performance 2 Responsiveness
Enhancement Multithreading enhances
Multithreading improves responsiveness in
performance by utilizing applications by separating
available CPU resources time-consuming tasks from
effectively, reducing the main thread, preventing
execution time. UI freezes.

3 Resource Utilization 4 Code Organization


Multithreading enables Multithreading facilitates
better resource utilization, better code organization
allowing servers to handle and modularity by dividing
multiple client requests complex tasks into smaller,
concurrently. manageable units.
Thread Pool
Definition: A Thread Pool is a programming concept and often a part of
concurrent programming libraries in various programming languages. It
refers to a pool of worker threads that are pre-created and kept ready to
perform tasks. The idea is to reuse these threads rather than creating a
new thread for each task, which can be more efficient in terms of
resource management.
Benefit of Thread Pooling

Resource Efficiency Improved Enhanced Scalability


Reduces the overhead of Responsiveness Management Adapts efficiently to varying
constantly creating and Ensures that tasks are Provides a controlled and workloads, handling surges
destroying threads. completed more quickly due efficient method for in task requests without
Preallocated threads in a to the availability of ready managing the lifecycle of creating performance
pool are reused for threads, preventing delays threads. The pool manages bottlenecks. This ensures
subsequent tasks, and improving overall thread creation, execution, that the system remains
minimizing the resource application responsiveness, and termination, improving responsive even under
consumption associated with particularly critical for resource utilization and heavy load, improving
thread creation. applications with large preventing uncontrolled overall performance.
numbers of short tasks. thread growth.
Thread Pooling in Java: Implementation
Runnable Interface Executor Interface
Represents a task to be executed concurrently by a thread. Manages threads and executes Runnable tasks. It abstracts
It declares a single method, run(), containing the code to be thread creation and scheduling, allowing focus on task logic.
executed.
Runnable Interface
To use Runnable, you typically create a class that implements the Runnable interface and overrides the run() method
Executor Interface
Manages threads and executes Runnable tasks. It abstracts thread creation and scheduling, allowing focus on task logic.
InterruptedException:
Handling Thread
Interruptions
Propagate the Exception Restore the Interrupt
Allow the InterruptedException Preserve the interruption by
to propagate up the call stack, calling the interrupt() method
letting the caller handle the again so that the code higher
interrupt. up the call stack can see that
an interrupt was issued.

Custom Exception Handling


Create custom exceptions to handle interrupts in a specific way,
allowing for additional work before terminating the thread.
Propagate the Exception
Allow the InterruptedException to propagate up the call stack, letting the caller handle the interrupt.
Restore the Interrupt
Preserve the interruption by calling the interrupt() method again so that the code higher up the call stack can see that an
interrupt was issued.
Custom Exception Handling
Letʼs create a custom checked exception called CustomInterruptedException and then we can throw our
CustomInterruptedException when the thread is interrupted
Thread Pooling: Code Demo

Figure 1: The result of running program without ThreadPooling


Thread Pooling: Code Demo

Figure 2: The result of running program with ThreadPooling

You might also like