0% found this document useful (0 votes)
4 views10 pages

Threads in Java

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

Threads in Java

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

THREADS IN JAVA

 Program: An executable file containing a set of instructions and passively stored on disk.
 When a program is loaded into the memory and becomes active, the program becomes a
process.
 Process: It is a program is in execution. When a process starts, requires some essential
resources such as registers, program counter, and stack, it is also assigned memory and
resources.
 One program can have multiple processes. For example, the Chrome browser creates a
different process for every single tab.
• Thread: It is the smallest unit of execution within a process. It is a path followed during
a program’s execution.
• Threads are lightweight subprocesses, representing the smallest unit of execution with
separate paths.
• A process can have one or more threads. For example, in the Microsoft Word app, a
thread might be responsible for spelling checking and the other thread for inserting text
into the doc.
• Main advantage of multiple threads is efficiency (allowing multiple things at the same
time). Additionally, multithreading ensures quick response, as other threads can
continue execution even if one gets stuck, keeping the application responsive.
Threads in Java
• Multithreading in operating systems is similar to a person with
multiple hands.
• In this comparison, think a Process as a Person: A process
represents an individual person and Threads as Hands: Threads
are analogous to the person's hands.
• Think of it like a person with multiple hands working together
simultaneously to perform different parts of the same job to
complete it faster. For ex, to cook a dish, a person uses both
hands for performing different tasks—such as stirring a pot with
one hand while chopping vegetables with the other. The above
example
• A effectively
multithreaded illustrating
process how a multiple
can execute task canthreads
be performed by
concurrently, each handling a
utilizing different
separate part of a"hands"
job at the(threads) to work
same time. on separate
Example: parts ofeach
Downloading a part of a webpage
job at the same
simultaneously usingtime, maximizing
separate threads,efficiency
effectivelywithin a single
speeding up the overall download
process. Downloading a webpage using multithreading—where one thread can focus on
downloading plain text, another can download all images simultaneously, while third
thread downloads ads, if any.
This parallelism enhances efficiency and responsiveness. However, just as a
person must coordinate their hands to avoid conflicts, multithreaded processes
require careful synchronization to manage shared resources and prevent issues
like race conditions. In both scenarios, the "person" (process) maintains overall
control and coordination, ensuring that all “hands" (threads) work together
harmoniously to achieve the desired outcomes. Each “hand” is called a thread,
and they help make programs run more efficiently.

Key points of the analogy:


•Multiple tasks: Just like a person with multiple hands can perform several
actions at once, a multithreaded program can execute multiple tasks
concurrently within a single application.
•Shared resources: Each "hand" (thread) in the analogy shares the same body
(program) and can access the same resources (data) within the application,
similar to how threads within a process share memory.
•Concurrent execution: While not necessarily truly parallel in all cases, the
multiple threads can appear to be working simultaneously by switching rapidly
between them, like a person quickly moving between different tasks with their
multiple hands.
Process vs
Main differences between process and thread:
thread
🔹 Processes are usually independent, while threads exist as subsets of a
process.
🔹 Each process has its own memory space. Threads that belong to the same
process share the same memory.
🔹 A process is a heavyweight operation. It takes more time to create and
terminate.
🔹 Context switching is more expensive between processes.
🔹 Inter-thread communication is faster for threads.
Threads in Java
Multithreading is a feature in operating systems that allows a
program to do several tasks at the same time. Multithreading
makes your computer work better by using its resources more
effectively, leading to quicker and smoother performance for
applications like web browsers, games, and many other
programs
How Does you use every day.
Multithreading Work?
Multithreading works by allowing a computer’s processor to handle multiple tasks at
the same time. Even though the processor can only do one thing at a time, it switches
between different threads from various programs so quickly that it looks like
everything is happening all at once.
Processor Handling : The processor can execute only one instruction at a time, but it
switches between different threads so fast that it gives the illusion of simultaneous
execution.
Thread Synchronization : Each thread is like a separate task within a program. They
share resources and work together smoothly, ensuring programs run efficiently.
Efficient Execution : Threads in a program can run independently or wait for their turn
to process, making programs faster and more responsive.
A thread in Java is the direction or
What is a Thread in Java? path that is taken while a program is
being executed. Generally, all the
programs have at least one thread,
known as the main thread, that is
provided by the JVM or
Java Virtual Machine at the starting of
the program’s execution. At this point,
when the main thread is provided, the
main() method is invoked by the main
thread.
A thread is an execution thread in a
program. Multiple threads of
execution can be run concurrently by
an application running on the Java
Virtual Machine. The priority of each
thread varies. Higher priority threads
are executed before lower priority
threads.
Threads have some common
information, such as data segment,
code segment, files, etc., that is
shared to their peer threads. But
contains its own registers, stack, and
Java Thread Benefits
1.Java Threads are lightweight compared to
processes, it takes less time and resource to create
a thread.
2.Threads share their parent process data and
code
3.Context switching between threads is usually
less expensive than between processes.
4.Thread intercommunication is relatively easy
than process communication.
Java provides two ways to create a thread
programmatically.
5.Implementing
the java.lang.Runnable interface.
6.Extending the java.lang.Thread class.

You might also like