0% found this document useful (0 votes)
10 views12 pages

Java Multithreading Basics Explained

Uploaded by

thaparohan2019
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)
10 views12 pages

Java Multithreading Basics Explained

Uploaded by

thaparohan2019
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

MULTITHREADING

IN
JAVA
Java Threads
A thread is a lightweight process and the
smallest unit of processing. In Java, each
thread runs parallel to each other.

Java supports multithreading through its


[Link] class and the
[Link] interface.

Threads can be used to perform complicated


tasks in the background without interrupting
the main program.
Why Multithreading ?
Increased Responsiveness: Threads can
run while other tasks are in progress.

Improved Performance: Efficient use of


CPU resources.

Simplified Modeling: Useful for modeling


asynchronous events.
Creating Threads in Java
Threads can be created either by extending
thread class or implementing the Runnable
Interface.
1. By Extending the Thread Class:
Extending the Thread class and overriding
the run() method allows you to define the
code that constitutes the new thread.
Extending Threads Class

Explanation: The start() method begins the


execution of the thread, which calls the run()
method. Each thread will print its ID and a
value.
Creating Threads in Java
1. By Implementing the Runnable Interface:
Implementing the Runnable interface is a
more flexible way to create a thread as it
allows your class to extend another class.
Implementing Runnable

Explanation: We create a Runnable object and


pass it to a Thread object. The start() method
of the Thread class begins the execution of the
run() method.
Thread States
A thread can be in one of the following states:
1. New: The thread instance has been
created but not yet started.
2. Runnable: The thread is ready to run and
waiting for CPU cycles.
3. Blocked: The thread is waiting for a
monitor lock to enter or re-enter a
synchronized block/method.
4. Waiting: The thread is waiting indefinitely
for another thread to perform a particular
action.
5. Timed Waiting: The thread is waiting for
another thread to perform a particular
action within a specified waiting time.
6. Terminated: The thread has finished its
execution.
Concurrency Problems
Because threads run at the same time as other
parts of the program, there is no way to know
in which order the code will run.

When the threads and main program are


reading and writing the same variables, the
values are unpredictable. The problems that
result from this are called concurrency
problems.
Thread Syncronization
To prevent thread interference and memory
consistency errors, synchronization is used.
1. Synchronized Method:
Synchronizing methods ensures that only
one thread can access the method at a
time.
2. Synchronized Block:
Synchronizing only the critical section of
the code rather than the entire method
improves performance.
Synchronized Method
Thank You

You might also like