0% found this document useful (0 votes)
59 views36 pages

Threading

The document discusses key concepts related to multithreading in Java including: 1) The differences between multitasking and multithreading, and how multithreading allows multiple threads of the same process to execute simultaneously. 2) The life cycle of a thread in Java which includes states like new, runnable, running, blocked, and dead. 3) How to create threads in Java by extending the Thread class or implementing the Runnable interface. 4) Inter-thread communication techniques like wait(), notify(), and notifyAll() methods.

Uploaded by

ruchisuru20001
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)
59 views36 pages

Threading

The document discusses key concepts related to multithreading in Java including: 1) The differences between multitasking and multithreading, and how multithreading allows multiple threads of the same process to execute simultaneously. 2) The life cycle of a thread in Java which includes states like new, runnable, running, blocked, and dead. 3) How to create threads in Java by extending the Thread class or implementing the Runnable interface. 4) Inter-thread communication techniques like wait(), notify(), and notifyAll() methods.

Uploaded by

ruchisuru20001
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/ 36

Multithreading

Differences between multi-threading and multitasking,

• The basic difference between Multitasking and multithreading is


that Multitasking allows CPU to perform multiple tasks (program,
process, task, threads) simultaneously
whereas, Multithreading allows multiple threads of the same process
to execute simultaneously.
Multithreading
Thread in Java?

• A thread in Java is the path followed when executing a program. All


Java programs have at least one thread, known as the main thread,
which is created by the Java Virtual Machine (JVM) at the program’s
start, when the main() method is invoked.
Java Thread vs. Java Process
• A Java process is a program in execution. A Java thread is a subset of a
Java process.

• A Java process consists of multiple threads and a Java thread is often


regarded as a light-weight process

• While a Java process has its own address space, a Java thread uses the
process’ address space and shares it with other threads of that process.
• A thread can communicate with other threads of the same process
using methods like wait(), notify(), notifyAll(). Global variables can
also be used to pass data between threads. On the other hand, a process
can only communicate with other process by using inter-process
communication techniques like sockets, files, etc.
thread life cycle in java
• Life Cycle of Thread in Java is basically state transitions of a thread
that starts from its birth and ends on its death.
• A thread is a path of execution in a program that enters any one of the
following five states during its life cycle. The five states are as follows:
• 1. New
• 2. Runnable
• 3. Running
• 4. Blocked (Non-runnable state)
• 5. Dead
• .1. When sleep() method is invoked on a thread to sleep for specified
time period, the thread is out of queue during this time period. The
thread again reenters into the runnable state as soon as this time period
is elapsed.
• 2. When a thread is suspended using suspend() method for some time
in order to satisfy some conditions. A suspended thread can be revived
by using resume() method.
• 3. When wait() method is called on a thread to wait for some time. The
thread in wait state can be run again using notify() or notifyAll()
method.
• New (Newborn State): When we create a thread object using Thread
class, thread is born and is known to be in Newborn state. That is,
when a thread is born, it enters into new state but the start() method
has not been called yet on the instance.

• In other words, Thread object exists but it cannot execute any


statement because it is not an execution of thread. Only start() method
can be called on a new thread; otherwise,
an IllegalThreadStateException will be thrown.
2. Runnable state: Runnable state means a thread is ready for
execution. When the start() method is called on a new thread, thread
enters into a runnable state.

In runnable state, thread is ready for execution and is waiting for


availability of the processor (CPU time). That is, thread has joined
queue (line) of threads that are waiting for execution.
• Running state: Running means Processor (CPU) has allocated time
slot to thread for its execution. When thread scheduler selects a thread
from the runnable state for execution, it goes into running state.
• Blocked state: A thread is considered to be in the blocked state when
it is suspended, sleeping, or waiting for some time in order to satisfy
some condition.

• Dead state: A thread dies or moves into dead state automatically when
its run() method completes the execution of statements. That is, a
thread is terminated or dead when a thread comes out of run() method.
A thread can also be dead when the stop() method is called.
Creating threads in Java
• We know that every Java program has at least one thread called main
thread. When a program starts, main thread starts running
immediately.

• Apart from this main thread, we can also create our own threads in a
program that is called child thread. Every child threads create from its
main thread known as parent thread.
Creating Multiple Threads in Java

• A thread in Java can be created in the following two ways:

• Extending java.lang.Thread class


• Implementing Runnable interface
Extending java.lang.Thread class

• In this case, a thread is created by a new class that extends the Thread
class, creating an instance of that class. The run() method includes the
functionality that is supposed to be implemented by the Thread.
Implementing Runnable interface
Synchronization
• Synchronization in java is the capability to control the access of
multiple threads to any shared resource. In the Multithreading concept,
multiple threads try to access the shared resources at a time to produce
inconsistent results. The synchronization is necessary for reliable
communication between threads.
Inter-thread communication
• Inter-thread communication in Java is a technique through which
multiple threads communicate with each other.
• There are several situations where communication between threads is
important.
• For example, suppose that there are two threads A and B. Thread B
uses data produced by Thread A and performs its task.
• If Thread B waits for Thread A to produce data, it will waste many
CPU cycles. But if threads A and B communicate with each other
when they have completed their tasks, they do not have to wait and
check each other’s status every time.
• Thus, CPU cycles will not waste. This type of information exchanging
between threads is called inter-thread communication in Java.
How to achieve Inter thread communication in Java

• Inter thread communication in Java can be achieved by using three


methods provided by Object class of java.lang package. They are:
1.wait()
2.notify()
3.notifyAll()
• These methods can be called only from within a synchronized method
or synchronized block of code otherwise, an exception
named IllegalMonitorStateException is thrown.
wait() Method in Java

• wait() method in Java notifies the current thread to give up the monitor
(lock) and to go into sleep state until another thread wakes it up by
calling notify() method. This method throws InterruptedException.
• A monitor is an object which acts as a lock. It is applied to a thread
only when it is inside a synchronized method.
• 2. Only one thread can use monitor at a time. When a thread acquires a
lock, it enters the monitor.
• 3. When a thread enters into the monitor, other threads will wait until
first thread exits monitor.
notify() Method in Java

• The notify() method wakes up a single thread that called wait()


method on the same object. If more than one thread is waiting, this
method will awake one of them. The general syntax to call notify()
method is as follows:

• Public final void notify()


notify All() Method in Java

• The notify All() method is used to wake up all threads that called
wait() method on the same object. The thread having the highest
priority will run first.
daemon threads
• Daemon threads is a low priority thread that provide supports to user
threads. Daemon Threads in Java are also known as Service
Provider Threads.
Example
• Let us consider a situation where the main thread is running with low
memory; then what JVM will do, it will run the Garbage Collector
(GC) to destroy the unreachable objects so that the memory will be
freed up and with this free memory, the main thread can continue its
execution. So here this GC is a daemon thread that provides the
service of cleaning memory for the main thread so that the main thread
can continue its execution without any interruption. Hence the main
objective of daemon threads is to provide services to main/user
threads.
• The life cycle of daemon threads depends on user threads. It
provides services to user threads for background supporting tasks.
• When all the user threads terminate, JVM terminates Daemon
threads automatically.
• It is a thread with the lowest priority possible.
• JVM won’t be concerned about whether the Daemon thread is active
or not.
Methods for Daemon Thread

• void setDaemon(boolean status): To make a thread a daemon


thread. We can change the nature of a thread by using this method.
• boolean isDaemon(): Used to determine whether or not the current
thread is a daemon. If the thread is Daemon, it returns true;
otherwise false.
Thread groups
• ThreadGroup in java can be defined as a collection of threads created
to work as a unit, ThreadGroup in java is generally used when there is
a need to perform a combined operation on a group of threads;
ThreadGroup offers an efficient way to manage multiple threads.
• Constructors:

• public ThreadGroup(String name)


• public ThreadGroup(ThreadGroup parent, String name):
Generic class in java with example
• Java Generics allows us to create a single class, interface, and method that can be
used with different types of data (objects).

• Generics, as the name suggests, is a general way of defining methods,


definitions, and collections so that they are not dependent on the data type
supplied to them.
• They can function the same no matter what datatype they use.

• Advantages of Java Generics


• 1. Type-Safety: One can hold only a single type of objects in generics.
• 2. Type Casting Is Not Required: There is no need to typecast.
Java Generic Methods

• You do not always need to use generic classes when you are
unsure of the datatype of a class.
• You can simply go and declare a method with generics too
Bounded Types

• When you want to specify a particular type which is a subclass of


a particular class you can use Bounded Types. Bounded types
allow you to restrict the data type of the collection. For example,
if you write a generic with the extends keyword then you can use
bounded types.
Eg <T extends A>
• If A is Number class then you can use all types of data which are
of type Integer, Float, Double, etc.
Practice question.
• Write a Java program that creates two threads. The first thread should
print odd numbers from 1 to 10, while the second thread should print
even numbers from 2 to 10. Ensure that the threads run concurrently
and synchronize their outputs.

You might also like