Multithreading in Java
Multithreading Concept In JAVA
Threads can be created using two
mechanisms.
1. Thread class extension
2. Runnable
Interface implementation
Thread Class vs Runnable Interface:-
1. Multiple inheritances are not
supported in Java, so you can't extend the "Thread class" to extend
other classes. Other base classes can be extended from the class by
implementing the interface "Runnable."
2. The basic functionality of a
thread can be expanded by extending the Thread class, which has inherent
methods like interrupt() and yield().
3. If you use runnable, you'll get an
object that may be shared by multiple threads.
Multitasking :-
Multitasking is the process of
performing several tasks at the same time. We use multitasking to utilize the
CPU. There are two methods for multitasking:
- Process-based Multitasking (Multiprocessing)
- Thread-based Multitasking (Multithreading)
(A) Process-based Multitasking (Multiprocessing)
Each process has its own memory
address. To put it another way, each process has its own memory space. A
procedure is a heavy undertaking. Communication between processes has a
significant cost. Switching from one process to another takes time to save and
load registers, memory mappings, and update lists, among other things.
(B) Thread-based
Multitasking (Multithreading)
The address space for all threads is
the same. A thread is a very light material. The cost of communication between
threads is minimal.
HOW TO CREATE
MULTIPLE THREADS IN JAVA :-
In the Java language package, there
is a runnable interface and a class thread that may be
accessed. In Java, there are two approaches to create multithreading :-
- By Creating a Runnable Interface and
overriding a run() method:
It
is the process to utilize when a thread needs to be generated, and it is the
most recommended method to use.
- By creating a thread interface and overriding
a start() method:
A thread interface is implemented
when your process demands more functionality for a thread.
CYCLE OF THREAD IN
VARIOUS PHASES:-
(a) New: A
thread's life cycle begins in the new state. It will stay in this condition
until you run the start () method on it.
(b) Runnable: When
the start () method is called on a new thread, it becomes runnable. In a Java
multithreading program, each thread is given a set amount of time. Each thread
runs for a short period of time before pausing and providing a new thread to
the CPU so that other threads can run. All threads are ready to run, waiting for
the CPU, and the currently executing thread is in a runnable state once this is
accomplished.
(c) Running: The
stage is switched to the "running" stage after the thread execution
has begun. The scheduler chooses one thread from the group of threads to begin
the application's execution.
(d) Waiting: A
thread is said to be waiting if it is waiting for another thread to complete a
task. The thread is still alive at this point. For example, a thread gets
blocked as it waits for I/O to complete. The Thread Scheduler oversees
reactivating and scheduling a blocked or waiting thread. A thread in this state
can't move forward unless it's moved to a working state. In these conditions,
no CPU cycle is utilized by any thread.
(e) Terminated/Dead:
The "dead" stage refers to the point at which the thread is
terminated. When a thread is switched from running to finished processing, it
is terminated and is considered "dead."
BENEFITS OF
MULTITHREADING IN JAVA:-
In Java, a multithreading program
allows many components of a program to run at the same time. Threads are
lightweight processes that are readily available throughout the procedure. As a
result, a Java multithreading program maximizes CPU
utilization through multitasking. The following are some of the benefits
of multithreading in Java :-
- Multithreading applications in Java make it
easier to conduct numerous computing activities at the same time.
- Multithreading applications improve the
program's responsiveness. When many threads are active, separate threads
are executed, causing the program to freeze. It maintains the application
going by running other pending tasks at the same time.
- Multithreading program improve the speed with
which a server responds. It ensures that huge and complex requests are not
frozen, and that generating numerous threads in Java does not prevent
other requests from being processed. It improves the server's total
output.
- Multithreading in Java reduces system usage by
making threads easier to maintain, manage, and create with less overhead.
- In Java, multithreading enhances the program
structure by making it simpler and more accessible. These reduced threads
can be utilized in high-server-class media applications to easily
adjust or improve the structure of these intricate structures.
- In Java, multithreading allows for
synchronization. It allows for faster data sharing. All of this is made
feasible by sharing space with someone who has a similar data address.
CONCLUSION :-
Multithreading in Java is an
important feature that every Java developer should be familiar with. It aids in
the efficiency of the program and decreases the use of storage resources. It
allows the CPUs to be used to their full potential, regardless of their
complexity. If you want to learn more about threads, you'll need to know about
the Thread class and the Runnable interface, as both provide a path and make
threads popular among programmers
REFERENCES :-
https://www.jigsawacademy.com/blogs/java/multithreading-in-java
https://www.geeksforgeeks.org/multithreading-in-java
https://www.javatpoint.com/multithreading-in-jav
https://blog.knoldus.com/multithreading-in-java/
https://medium.com/swlh/java-multithreading-b8dd4771a902
GROUP MEMBERS: -
SAHIL BHAT (02)
AKASH SALUNKHE (04)
MANDAR SALVI (05)
SAMEET SHAIKH (06)
SHRINIVAS SARAF (11)
SAMVEDH SHETTY (17)
Interesting
ReplyDeleteThanks for the Knowledge!
ReplyDeleteVery informative! π
ReplyDeleteGreat information π
ReplyDeleteFound informative. Jiven jiven turan lok follow karde
ReplyDeleteHelpfulππ»
ReplyDeletevery helpful
ReplyDeleteexcellent work done
ReplyDeleteVery well explained about the concept of multithreading
ReplyDeleteNicely explained about the multithreading concept, their types and advantages
ReplyDeleteNice π
ReplyDeletevery informative
ReplyDeleteVery informative blog
ReplyDeleteGreat work done
ReplyDelete