Threading
Threading
• 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.
• 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
• 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
• 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 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
• 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