Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Multitheading in Java

Multithreading class presentation notes

Uploaded by

subhashissohail
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)
21 views

Multitheading in Java

Multithreading class presentation notes

Uploaded by

subhashissohail
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/ 18

What is multithreading?

• Multithreading in Java is a process of


executing multiple threads simultaneously.
• Multithreading is a subset of Multitasking.

• Multitasking means that the operating


system (OS) allows more than one task to be
executed concurrently, regardless of whether
the tasks use the same program or different
programs.
• We use multitasking to utilize the CPU.
Advantages of Multithreading
• 1) It doesn't block the user because
threads are independent and you can
perform multiple operations at the same
time.
• 2) It can perform many operations
together, so it saves time.
• 3) Threads are independent, so it doesn't
affect other threads if an exception occurs
in a single thread.
What is a thread?
• A thread is a lightweight
subprocess, the smallest unit of
processing. It is a separate path
of execution.
• Threads are independent. If
there occurs exception in one
thread, it doesn't affect other
threads. It uses a shared memory
area.
Processes are basically the programs that are
dispatched from the ready state and are scheduled in
the CPU for execution.
Reference: https://www.javatpoint.com/multithreading-in-java
Java Thread class
• In Java, the Thread class is a
fundamental tool for working with
threads.
• It equips developers with the means
to create threads and perform various
operations on them.
• This class extends the basic
functionality provided by the Object
class and also implements the
Runnable interface, allowing for
versatile and concurrent
programming.
Life Cycle of a Thread
1. New: The thread is newly created but has not yet
started its execution.
2. Active: The thread is actively executing its code
or performing its task.
3. Blocked / Waiting: The thread is temporarily
suspended because it's waiting for some event or
resource, such as I/O, to become available. It's in
a blocked state until the condition is met.
4. Timed Waiting: Similar to the "Waiting" state,
but with a specific time limit. The thread waits
for a specified duration and then automatically
transitions to the "Active" state.
5. Terminated: The thread has completed its
execution and has terminated. Once terminated,
a thread cannot be restarted.
Java Thread Methods
• start() : It is used to start the execution of the thread.
• run() : It is used to do an action for a thread.
• sleep() : It sleeps a thread for the specified amount of time.
• currentThread() : It returns a reference to the currently executing
thread object.
• join() : It waits for a thread to die.
• getPriority(): It returns the priority of the thread.
• setPriority(): It changes the priority of the thread.
Java Thread Methods(contd).
• getName(): It returns the name of the thread.
• setName(): It changes the name of the thread.
• getId(): It returns the id of the thread.
• isAlive(): It tests if the thread is alive.
• yeild(): It causes the currently executing thread object to pause and
allow other threads to execute temporarily.
• suspend():It is used to suspend the thread.
• resume():It is used to resume the suspended thread
Java Thread Methods(contd).
• stop():It is used to stop the thread.
• destroy():It is used to destroy the thread group and all of its
subgroups.
• synchronized(): The synchronized keyword is used to provide
mutual exclusion, ensuring that only one thread can execute a
synchronized block or method at a time. It is primarily used to
prevent race conditions and ensure thread safety when multiple
threads access shared resources or critical sections.
Ways of Running a Code in Threads

• The major difference is that when a class extends the Thread class,
you cannot extend any other class, but by implementing the
Runnable interface, it is possible to extend from another class as well,
like: class MyClass extends OtherClass implements Runnable.
Execution of code in a thread – extending
Thread Class
Execution of code in a thread – implementing
Runnable
Issues while Running a Code in Threads
Concurrency Issues: Threads execute concurrently with other parts of
the program, making it unpredictable to determine the order in which
the code will run.
Example:
Output is different
In diff. iterations.
Solution for Concurrency Issues
Use join():
Solution for Concurrency Issues
Use isAlive():
Issues while Running a Code in Threads
Race-around Issues: Threads execute parallel to one another, making it
unpredictable to determine the order in which the threads will run.
Example:
Output is different
In diff. iterations.
Solution for Race-Around Issues
• Use synchronized
synchronized block prevent
race conditions by allowing
only one thread to execute
them at a time, ensuring that
shared data is accessed and
modified safely.
Additional Resources
For more details, try to visit the following sites apart from books:
a) https://www.informit.com/articles/article.aspx?p=1315432
b) https://www.tutorialspoint.com/java/java_multithreading.htm
c) https://www.javatpoint.com/multithreading-in-java
d) https://www.geeksforgeeks.org/multithreading-in-java/
Thank you

You might also like