Thread in Java
Thread in Java
Thread in Java
Threads
• Overview
• Multicore Programming
• Multithreading Models
• Thread Libraries
• Implicit Threading
• Threading Issues
Outcomes
•Relate the role of threads in process
management system.
•Solve the issues of threads in multicore
programming and multithreading model.
•Survey similarities and the differences
between process and threads.
What is Thread?
• A thread is a flow of execution through the process code,
with its own program counter that keeps track of which
instruction to execute next, system registers which hold
its current working variables, and a stack which contains
the execution history.
• A thread shares with its peer threads few information like
code segment, data segment and open files.
What is Thread?
• A thread is a basic unit of CPU utilization; it comprises a
thread ID, a program counter, a register set, and a stack.
• It shares with other threads belonging to the same process
its code section, data section, and other operating-system
resources, such as open files and signals.
• A traditional (or heavyweight) process has a single thread
of control.
• If a process has multiple threads of control, it can perform
more than one task at a time.
Characteristics of Threads
• A thread is also called a lightweight process.
• Threads provide a way to improve application performance
through parallelism.
• Threads represent a software approach to improving
performance of operating system by reducing the
overhead thread is equivalent to a classical process.
• Each thread belongs to exactly one process and no thread
can exist outside a process.
• Each thread represents a separate flow of control.
• Threads have been successfully used in implementing
network servers and web server.
• They also provide a suitable foundation for parallel
execution of applications on shared memory
multiprocessors.
Characteristics of Threads
The following figure shows the working of a single-threaded and a
multithreaded process
Comparison between Process
and Threads
Process Thread
Process is heavy weight or resource intensive. Thread is light weight, taking lesser
resources than a process.
Process switching needs interaction with Thread switching does not need to interact
operating system. with operating system.
If one process is blocked, then no other process While one thread is blocked and waiting, a
can execute until the first process is unblocked. second thread in the same task can run.
Multiple processes without using threads use Multiple threaded processes use fewer
more resources. resources.
In multiple processes each process operates One thread can read, write or change
independently of the others. another thread's data.
• In certain situations, a single application may be required to perform
• several similar tasks. For example, a web server accepts client requests for
• web pages, images, sound, and so forth. A busy web server may have several
• (perhaps thousands of) clients concurrently accessing it. If the web server ran
• as a traditional single-threaded process, it would be able to service only one
• client at a time, and a client might have to wait a very long time for its request
• to be serviced.
• One solution is to have the server run as a single process that accepts
• requests. When the server receives a request, it creates a separate process
• to service that request. In fact, this process-creation method was in common
• use before threads became popular. Process creation is time consuming and
• resource intensive, however. If the new process will perform the same tasks as
• the existing process, why incur all that overhead? It is generally more efficient
• to use one process that contains multiple threads. If the web-server process is
• multithreaded, the server will create a separate thread that listens for client
• requests. When a request is made, rather than creating another process, the
• server creates a new thread to service the request and resume listening for
• additional requests.
Multiple server Architecture
Advantages of
Thread
• Responsiveness. Multithreading an interactive application
may allow a program to continue running even if part of it
is blocked or is performing a lengthy operation, thereby
increasing responsiveness to the user.
• Resource sharing. Processes can only share resources
through techniques such as shared memory and message
passing.
• Economy. Allocating memory and resources for process
creation is costly. Because threads share the resources of
the process to which they belong, it is more economical to
create and context-switch threads.
• Scalability. The benefits of multithreading can be even
greater in a multiprocessor architecture, where threads
may be running in parallel on different processing cores.
Types of Thread
S.N
User-Level Threads Kernel-Level Thread
.
User-level threads are faster to create Kernel-level threads are slower to create
1
and manage. and manage.
User-level thread is generic and can run Kernel-level thread is specific to the
3 on any operating system. operating system.
Signals are used in UNIX systems to notify a process that a particular event has
occurred
- CTRL-C is an example of an asynchronous signal that might be sent to a process
An asynchronous signal is one that is generated from outside the process that
receives it
- Divide by 0 is an example of a synchronous signal that might be sent to a process
A synchronous signal is delivered to the same process that caused the signal
to occur
• All signals follow the same basic pattern:
A signal is generated by particular event
The signal is delivered to a process
The signal is handled by a signal handler (all signals are handled exactly once)
Thread Pools
• In applications where threads are repeatedly being created/destroyed thread
pools might provide a performance benefit
- Example: A server that spawns a new thread each time a client connects to the
system and discards that thread when the client disconnects!
A thread pool is a group of threads that have been pre-created and are available
to do work as needed
- Threads may be created when the process starts
A thread may be kept in a queue until it is needed
After a thread finishes, it is placed back into a queue until it is needed again
Avoids the extra time needed to spawn new threads when they’re needed
Thread Pools