You are on page 1of 29

Multithreading in Java

Multitasking Introduction
The process of executing several tasks simultaneously is called ‘Multitasking‘. There are 2 types of
multitasking.
1) Process based Multitasking.
2) Thread based Multitasking.

1) Process based Multitasking: Executing several tasks simultaneously where each task is a separate
independent process is called ‘Process based Multitasking‘.

Example: While writing a java program in the editor, we can run MP3 player. At the same time we can
download a file from the net. All these tasks are executing simultaneously and independent of each
other. Hence it is process based Multitasking.

Process based Multitasking is best suitable at operating system level.


2) Thread based Multitasking: Executing several tasks simultaneously where each task
is a separate independent part of the same program is called ‘Thread based
Multitasking‘.

Example: assume there is code of 10000 lines, now we can divide this program into two
thread for 5000 lines of independent code. Due to which exception time is reduced &
performance improved.

 This type of multitasking is best suitable at programmatic level.


 Java provides in-built support for thread based multitasking by providing rich
library (Thread, ThreadGroup, Runnable ...Etc.).
 Whether it is Process based or Thread based the main objective of multitasking
is to reduce response time and improve performance of the system.
 The main application area of multithreading is video games implementation, Multimedia
graphics etc.
What is a process?

A process consists of a memory space allocated by the operating system


that can contain one or more threads. The thread cannot exist on its own; it
must be a part of a process. A process remains running until all of the non-
daemon threads are done executing.

What is a thread?

 The thread is a lightweight sub-process and the smallest unit of execution.


 Each thread has a separate path of execution.
 It shares the memory area of the process.
Multithreading in Java

 Multithreading in java is a process of executing multiple threads simultaneously.


 Multiprocessing and multithreading, both are used to achieve multitasking.
 But we use multithreading than multiprocessing because threads share a common
memory area. They don't allocate separate memory area so saves memory, and
context-switching between the threads takes less time than process.

Advantages of Java Multithreading

1) It doesn't block the user because


threads are independent and you can
perform multiple operations at same time.
2) You can perform many operations
together so it saves time.
3) Threads are independent so it doesn't
affect other threads if exception occur in a
single thread.
4) It provides enhanced performance on
multi-processor machines.
5) Improvised GUI responsiveness.

HOW TO CREATE THREAD

There are two ways to create a thread:

1. By extending Thread class


2. By implementing Runnable interface.

Thread class:
Thread class provide constructors and methods to create and perform operations on a thread.
Thread class extends Object class and implements Runnable interface (it contains run() method
with empty implementation).

Commonly used Constructors of Thread class:


 Thread(): This allocates a new Thread object.
 Thread(String name): This constructs allocates a new
Thread object with assigning name.
 Thread(Runnable r): This allocates a new Thread of runnable object.
 Thread(Runnable r,String name): runnable object & assigning name.
Runnable interface:
The Runnable interface should be implemented by any class whose instances are intended to
be executed by a thread. Runnable interface have only one method named run().
public void run(): is used to perform action for a thread.
Starting a thread: start() method of Thread class is used to start a newly created thread. It
performs following tasks:
start() {
a) A new thread starts (with new callstack).
b) The thread moves from New state to the Runnable state.
c) When the thread gets a chance to execute, its target run() method will run.
}

1) Java Thread Example by extending Thread class


Output

THREAD SCHEDULER IN JAVA

 Thread scheduler in java is the part of the JVM that


decides which thread should run.
 There is no guarantee that which runnable thread
will be chosen to run by the thread scheduler. Hence
we can’t expect thread’s execution order & exact
output.
 Only one thread at a time can run in a single process.

The thread scheduler mainly uses preemptive or time slicing


scheduling to schedule the threads.
Possible outputs for above program

Difference between preemptive scheduling and time


slicing

Under preemptive scheduling, the highest priority task executes until it enters the waiting or
dead states or a higher priority task comes into existence. Under time slicing, a task executes
for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then
determines which task should execute next, based on priority and other factors.
Difference bw t.start() & t.run

If we use t.run() instead of t.start() in


above program then there is no
multithreading performed, it will be
normal execution of method call & will
get fixed output as shown right side.

Also in this case two threads are not


created, all job will be done by main
thread.

Therefore in the case of t.start() a new thread will be created which is responsible for the
excecution of run method. Therefore t.start() is responsible for creating thread.

Case 1: Here we are not defining run() method for thread Case 2: Here run() method without argument is
which is job of it. Even though if we are not defining run() called because its overridden from Thread class.
method, in this case compiler will call run() method of Thread So to call run(int i) we have explicitly call it by
class which has empty implementation & output for this method call i.e. t.run(3).
program also be empty i.e. blank.

Case 3: If we override test() method, then it will call


overridden test() method. So if we want to create thread we
have to explicitly call super.test() method in test() method.

Case 4: thread once created can’t create again..

Three possibilities of o/p for this programs as stated above, coz thread is created therefore no guaranty of output order.
2) Java Thread Example by implementing Runnable interface

If you are not extending the Thread class, your class object would not be treated as a thread
object. So you need to explicitly create Thread class object. We are passing the object of your
class that implements Runnable so that your class run() method may execute.
May be in Thread(Runnable r) code is something like below
Thread(Runnable r)
{
r.run();
….
}
Which way is better?
Ans: Implementing Runnable interface because Extending Thread class will not give you an
option to extend any other class. But if you implement Runnable interface you could extend
other classes in your class.
So depending on your design requirement you could use either of the methods.
NAMING THREAD AND CURRENT THREAD

Naming Thread

The Thread class provides methods to change and get the name of a thread. By default, each
thread has a name i.e. thread-0, thread-1 and so on. By we can change the name of the thread
by using setName() method. The syntax of setName() and getName() methods are given
below:

1. public String getName(): is used to return the name of a thread.


2. public void setName(String name): is used to change the name of a thread.

Example of naming a thread

Possibility 1

Possibility 2

Current Thread

The currentThread() method returns a reference of currently executing thread.


Priority of a Thread (Thread Priority):
Each thread have a priority. Priorities are represented by a number between 1 and 10. In most
cases, thread schedular schedules the threads according to their priority (known as
preemptive scheduling). But it is not guaranteed because it depends on JVM specification that
which scheduling it chooses.
3 constants defiend in Thread class:
1) public static int MIN_PRIORITY
2) public static int NORM_PRIORITY
3) public static int MAX_PRIORITY
Default priority of a thread is 5 (NORM_PRIORITY). The value of MIN_PRIORITY is 1 and the
value of MAX_PRIORITY is 10.
Note: the default priority only for the main Thread is 5. But for all the remaining Threads the
default priority will be inheriting from parent to child. That is whatever the priority parent has
by default the same priority will be for the child also.

Get and Set Thread Priority:


1) public final int getPriority(): java.lang.Thread.getPriority() method returns priority of
given thread.
2) public final void setPriority(int newPriority): java.lang.Thread.setPriority() method
changes the priority of thread to the value newPriority. This method throws
IllegalArgumentException if value of parameter newPriority goes beyond minimum(1)
and maximum(10) limit.
Note: Some platforms won’t provide proper support for thread priorities.
Life cycle of a Thread (Thread States)

 A thread can be in one of the five states.


 According to sun, there is only 4 states in thread life cycle in java new, runnable,
non-runnable and terminated. There is no running state. But for better understanding
the threads, we are explaining it in the 5 states.

The life cycle of the thread in java is


controlled by JVM. The java thread
states are as follows:

1. New
2. Runnable
3. Running
4. Non-Runnable (Blocked)
5. Terminated

1) New: The thread is in new state if you create an instance of Thread class but before the
invocation of start() method.
2) Runnable: The thread is in runnable state after invocation of start() method, but the thread
scheduler has not selected it to be the running thread.
3) Running: The thread is in running state if the thread scheduler has selected it.
4) Non-Runnable (Blocked): This is the state when the thread is still alive, but is currently
not eligible to run.
5) Terminated: A thread is in terminated or dead state when its run() method exits.
Thread Class Methods
1) yield():
 yield() method pauses the currently executing thread temporarily for giving a chance
to the remaining waiting threads of the same priority to execute.
 If there is no waiting thread or all the waiting threads have a lower priority then the
same thread will continue its execution.
 The yielded thread when it will get the chance for execution is decided by the thread
scheduler whose behavior is vendor dependent.
 It can only make a thread from Running State to Runnable State, not in wait or blocked
state.

In the right program child Thread always calling yield()


method and hence main Thread will get the chance more
number of times for execution.
Hence the chance of completing the main Thread first is
high.

Note: Some operating systems may not provide proper


support for yield() method.

2) Sleep: If a Thread don't


want to perform any
operation for a particular
amount of time then we
should go for sleep()
method.

Syntax of sleep() method in java:The Thread class provides two methods for sleeping a
thread:

1) public static void sleep(long miliseconds)throws InterruptedException


2) public static void sleep(long miliseconds, int nanos)throws InterruptedException

Note: while its sleeping there is chance the thread may get interrupted therefore we have to throw InterruptedException
Interrupting a Thread:

If any thread is in sleeping or waiting state (i.e. sleep()


or wait() is invoked), calling the interrupt() method on
the thread, breaks out the sleeping or waiting state
throwing InterruptedException. If the thread is not in
the sleeping or waiting state, calling the interrupt()
method performs normal behaviour and doesn't
interrupt the thread but sets the interrupt flag to true.
Let's first see the methods provided by the Thread
class for thread interruption.

The 3 methods provided by the Thread class for


interrupting a thread

1) public void interrupt()


2) public static boolean interrupted()
3) public boolean isInterrupted()

The isInterrupted() method returns the interrupted flag either true or false.

The static interrupted() method returns the interrupted flag after that it sets the flag to false if it is
true.

Note:

 Whenever we are calling interrupt() method we may not see the effect immediately, if
the target Thread is in sleeping or waiting state it will be interrupted immediately.
 If the target Thread is not in sleeping or waiting state then interrupt call will wait until
target Thread will enter into sleeping or waiting state. Once target Thread entered into
sleeping or waiting state it will effect immediately.
 In its lifetime if the target Thread never entered into sleeping or waiting state then there
is no impact of interrupt call simply interrupt call will be wasted.
2) Join(): If a Thread wants to wait until
completing some other Thread then we should go
for join() method.
Example1: If a Thread t1 executes t2.join() then t1
should go for waiting state until completing t2.

Every join() method throws InterruptedException,


which is checked exception hence compulsory we
should handle either by try catch or by throws Example2
keyword.
Otherwise we will get compiletime error.
Thread join methods are as follows:

1. public final void join()throws InterruptedException


2. public final void join(long ms) throws InterruptedException
3. public final void join(long ms,int ns) throws InterruptedException

1) public final void join(): This java thread join method puts the current thread on wait until
the thread on which it’s called is dead. If the thread is interrupted, it throws
InterruptedException.
2) public final void join(long millis): This java thread join method is used to wait for the
thread on which it’s called to be dead or wait for specified milliseconds. Since thread execution
depends on OS implementation, it doesn’t guarantee that the current thread will wait only for
given time.
3) public final synchronized void join(long millis, int nanos): This java thread join method
is used to wait for thread to die for given milliseconds plus nanoseconds.

Here, main Thread will wait until completing child Here, main Thread will wait until 2 sec for child Thread.
Thread. in this the output is sita Thread 5 times followed Else will continue execution
by Rama Thread 5 times.
Waiting of child Thread until completing main
Thread

Note: If main thread calls join() on child thread


object and child thread called join() on main
thread object then both threads will wait for each
other forever and the program will be
hanged(like deadlock if a Thread class join()
method on the same thread itself then the
program will be hanged ).

Thread class join() method on the same thread itself


Synchronization in Java

(Whenever there is same resource sharing Synchronization comes in picture )


Why Synchronization?
When we start two or more threads within a program, there may be a situation when multiple
threads try to access the same resource and finally they can produce unforeseen result due
to concurrency issues.
For example, if multiple threads try to write within a same file then they may corrupt the data because
one of the threads can override data or while one thread is opening the same file at the same time
another thread might be closing the same file.

Eg-2 in this E.g. both has joint account and at a same time both are performing
transactions. If wife succeeded to withdraw 450 then hubby is confused why
he is not able withdraw even bal is 500 Rs/-

So there is a need to synchronize the action of multiple threads and make sure that only
one thread can access the resource at a given point in time.

Synchronization

1) Synchronization in java is the capability to control the access of multiple threads to any
shared resource.
2) Java Synchronization is better option where we want to allow only one thread to
access the shared resource.
3) Synchronized is the keyword applicable for methods and blocks but not for classes
and variables.
4) If a method or block declared as the synchronized then at a time only one Thread is
allow to execute that method or block on the given object.
5) The main advantage of synchronized keyword is we can resolve date inconsistency
problems.
6) But the main disadvantage of synchronized keyword is it increases waiting time of the
Thread and effects performance of the system. Hence if there is no specific
requirement then never recommended to use synchronized keyword.
7) While a Thread executing any synchronized method the remaining Threads are not
allowed execute any synchronized method on that object simultaneously. But
remaining Threads are allowed to execute any non-synchronized method
simultaneously. [Lock concept is implemented based on object but not based on
method].
Concept of Lock in Java

1) Every object in java has a unique lock. Whenever we are using synchronized keyword
then only lock concept will come into the picture.
2) If a Thread wants to execute any synchronized method on the given object 1st it has
to get the lock of that object. Once a Thread got the lock of that object then it's allow
to execute any synchronized method on that object. If the synchronized method
execution completes then automatically Thread releases lock.
When to use Synchronization & non-Synchronization?

non-synchronous Operations Synchronus operation


whenever object state won't be chnaged.this whenever we are performing update operation, where state of
area can be accessed by any no of threads object chnaging. This type of operation should be performed at
simaltaniously. a time by only one thread.
E.g. read operation. E.g. add, remove, delete, replace
real time E.g. checking availability of seat real time E.g. Reserving seat

Thread Synchronization: There are two types of thread synchronization mutual exclusive
and inter-thread communication.
1) Mutual Exclusive
 Synchronized method.
 Synchronized block.
 Static synchronization.
2) Cooperation (Inter-thread communication in java)
1) Mutual Exclusive: Mutual Exclusive keep threads from interfering with one another while
sharing data. This can be done by three ways in java:
a) by synchronized method
b) by synchronized block
c) by static synchronization
a) Java synchronized method:
 If a method or block declared as the synchronized then at a time only one Thread is
allow to execute that method or block on the given object.
 When a thread invokes a synchronized method, it automatically acquires the lock for
that object and releases it when the thread completes its task.
In below E.g. we are calling Wish method of Display class from multiple threads having same object.

!) If we are not declaring wish() method as synchronized then both Threads will be executed simultaneously and we will get
irregular output.

2) If we declare wish() method as synchronized then the Threads will be executed one by one that is until completing the 1st
Thread the 2nd Thread will wait

Note: Here as t1 has lock of d object, so until it unlocks this lock t2 can’t access sync method.
Even though we declared wish() method as synchronized but we will get irregular
output in this case, because both Threads are operating on different objects. Here
t1 having lock of d1 & t2 having lock of d2 so both are allow in sync method.

Conclusion: If multiple threads are operating on multiple objects then there is no


impact of Synchronization. If multiple threads are operating on same java objects
then synchronized concept is required (applicable).

b) Static synchronization: If you make any static method as synchronized, the lock will be
on the class not on object.

Class level lock:


1) Every class in java has a unique lock. If a Thread wants to execute a static
synchronized method then it required class level lock.
2) Once a Thread got class level lock then it is allow to execute any static synchronized
method of that class.
3) While a Thread executing any static synchronized method the remaining Threads are
not allow to execute any static synchronized method of that class simultaneously.
Same rule applies to synchronized method if class have two sync methods then at a
time only one method is executed (see case study case 2 below).
4) But remaining Threads are allowed to execute normal synchronized methods,
normal static methods, and normal instance methods simultaneously.
5) Class level lock (for static method) and object lock (for normal synchronized
methods) both are different and there is no relationship between these two.
Same above E.g. if we make wish() method as static Synchronized then will get consistency.

Case Study: Case2:

From right side image shows class X with six thread object & their accessibility on class x methods. T1 can access m1(), but t2 & t3 can’t access m1() & m2()
respectively as t1 holds class lock. T4, t5, t6 can access m3, m4 & m5 methods respectively as those don’t require class lock, and they requires object lock.
c) Synchronized block in java

1. If very few lines of the code required synchronization then it's never recommended to
declare entire method as synchronized we have to enclose those few lines of the code
with in synchronized block.
2. The main advantage of synchronized block over synchronized method is it reduces
waiting time of Thread and improves performance of the system.

E.g. suppose you have 50 lines of code in your method, but


you want to synchronize only 5 lines, you can use
synchronized block. If you put all the codes of the method in
the synchronized block, it will work same as the synchronized
method.

1) Synchronized(this){}: To get lock of current object we can declare this synchronized


block If Thread got lock of current object then only it is allowed to execute this block.

2) Synchronized(b){}: To get the lock of a particular object 'b' we have to declare this
synchronized block. If thread got lock of 'b' object then only it is allowed to execute this
block.

3) Synchronized(Display.class){}:To get class level lock we have to declare this


synchronized block. If thread got class level lock of Display then only it allowed to
execute this block.

e.g of synchronized block

In above e.g. to get lock of current object we used Synchronized(this){}. Here first t1 gets
lock of object d & lock this object, t2 has to wait for t1 to release lock. Hence sync is done.
Case study case 1: if created multiple objects then it won’t work see e.g. below.

Here we are using both different object when t1.start() runs then t1 locks object d1, & when
t2.start() runs then t2 locks d2 object, here both thread locks different object hence Sync is
not performed coz no one has to wait for releasing lock. to overcome this problem use
Synchronized(Display.class){} which has class level lock.
E.g. using Synchronized(Display.class){}
FAQ
1) What is Race condition?
Ans: If multiple threads are operating simultaneously on same java object then there may be
a chance of data inconsistency problem, this is called Race Condition. We can overcome this
problem by using Synchronized keyword.
2) While a Thread executing a synchronized method on the given object is the remaining Threads
are allowed to execute other synchronized methods simultaneously on the same object?
Ans: No.
3) Is a Thread can hold more than one lock at a time?
Ans: Yes, up course from different objects. Example:

Here t1 has two locks i.e. lock first it will have lock of x then lock of y.
4) What is synchronized statement?
Ans: The statements which present inside synchronized method and synchronized block are
called synchronized statements. [Interview people created terminology].
Inter-thread communication in Java (performs only in synchronized area )
Inter-thread communication or Co-operation is all about allowing synchronized threads
to communicate with each other.
Two Threads can communicate with each other by using wait(), notify() and notifyAll()
methods.
These methods have been implemented as final methods in Object
class, so they are available in all the classes. All three methods can
be called only from within a synchronized context.
All the three methods are listed below

Methods Method & Description


1. public final void wait()throws InterruptedException Causes the current thread to wait until another
2. public final native void wait(long ms)throws InterruptedException thread invokes the notify().
3. public final void wait(long ms,int ns)throws InterruptedException
Wakes up a single thread that is waiting on this
4. public final native void notify() object's monitor.

Wakes up all the threads that called wait( ) on


5. public final void notifyAll()
the same object.

Here while executing wait() method any thread can interrupt waiting tread that’s why it throws
IE exception.
Right side is e.g. how we can access wait(),
notify() and notifyAll() methods from object class
to our program.

O/P

 The Thread which is required updation it has to call wait()


method on the required object then immediately the Thread
will entered into waiting state.
 The Thread which is performing updation of object, it is
responsible to give notification by calling notify() method.
 After getting notification the waiting Thread will get those
updations.
Here in side e.g. if am not acknowledge by postman then every time I will check
letter box, but once I informed postman to let me know for letter update then I won’t
have to check repetitively. So here I want updation so I will be in wating state &
postman will notify me once he drops letter. So repetitively checking stops.
1) wait() method

 Causes current thread to release the lock and wait until either another thread invokes
the notify() method or the notifyAll() method for this object, or a specified amount of
time has elapsed.
 The current thread must own this object's monitor, so it must be called from the
synchronized method only otherwise it will throw exception.

2) notify() method 3) notifyAll() method

Wakes up a single thread that is waiting on this object's monitor. If Wakes up all threads that are waiting on this
any threads are waiting on this object, one of them is chosen to be object's monitor.
awakened. The choice is arbitrary and occurs at the discretion of
the implementation. Syntax: public final void notifyAll()
Syntax: public final void notify()

Understanding the process of inter-thread communication

The point to point explanation of the above diagram is as follows:

1. Threads enter to acquire lock.


2. Lock is acquired by on thread.
3. Now thread goes to waiting state if you call
wait() method on the object. Otherwise it
releases the lock and exits.
4. If you call notify() or notifyAll() method, thread
moves to the notified state (runnable state).
5. Now thread is available to acquire lock.
6. After completion of the task, thread releases the
lock and exits the monitor state of the object.

Why wait(), notify() and notifyAll() methods are defined in Object class not Thread class?
Ans:-It is because they are related to lock and object has a lock.
Is Thread
Except these (wait(),notify(),notifyAll()) methods there is no other place(method) Method
Releases Lock?
where the lock release will be happen.
yield() No
join() No
sleep() No
Once a Thread calls wait(), notify(), notifyAll() methods on any object then it wait() Yes
releases the lock of that particular object but not all locks it has. notify() Yes
notifyAll() Yes

Figure shows how these three methods executes


Here consider below e.g. without wait() method:

Case 1: using nothing

Multiple possibilities of o/p

Case 2: using sleep() method for 10 sec, in this case we’ll get expected o/p but unnecessarily our main thread has to wait for 9 sec
coz child thread completes its calculation within Nano seconds..

Will get fixed o/p

Case 3: using join() Method here also we’ll get expected result, but what if there is 1 crore line of code is there below for loop.
Then here also main thread has to wait to complete those 1 crore lines of code. So join() is not recommended.

Will get fixed o/p

Case 4: If you are waiting for updation then it is recommended to call wait() & notify() method. Note: wait() notify() works only in
synchronized area. Hence declared in sync blocks.

Case 1: If main thread got


chance to execute
Case 4:

Case 2: If Child thread got chance to


execute or if main thread sleeping.

Here main thread will be in forever waiting state as it


missed notify() call.

Case 4:

Case 3: to overcome above situation call


wait() method mentioning particular time,
So that main thread will wait for 10 sec & if
not got notify() call from child thread it will
continue further.

Notification not given but main thread waited for 10


sec hence got full o/p
Producer consumer problem:

 Producer(producer Thread) will produce the items to the


queue and consumer(consumer thread) will consume the
items from the queue. If the queue is empty then consumer
has to call wait() method on the queue object then it will
entered into waiting state.
 After producing the items producer Thread call notify() method
on the queue to give notification so that consumer Thread will
get that notification and consume items.

Note: On which object we are calling wait(), notify()


and notifyAll() methods that corresponding object
lock we have to get but not other object locks.
Dead lock

If 2 Threads are waiting for each other forever (without end) such type of situation (infinite
waiting) is called dead lock.
Deadlock can occur in a situation when a thread is waiting
for an object lock, that is acquired by another thread and
second thread is waiting for an object lock that is acquired
by first thread. Since, both threads are waiting for each
other to release the lock, the condition is called deadlock.
There are no resolution techniques for dead lock but several prevention (avoidance)
techniques are possible.
Synchronized keyword is the cause for deadlock hence whenever we are
using synchronized keyword we have to take special care.
In the side e.g.
 If thread "A" calls methodA and thread "B" calls methodB, then: lockA
will be aquired from threadA and lockB from threadB.
 Then threadA will have to wait for lockB while threadB waits for lockA
from the recently blocked threadA.
This is because the objects are locked in different order. This is one of the
most common reasons of deadlocks, so if you want to avoid them, be sure that
the locks are acquired in order. Consider below e.g.

If method1() and method2() both will be called by two or Now there would not be any deadlock because both
many threads , there is a good chance of deadlock because if methods are accessing lock on Integer and String class
thread 1 acquires lock on Sting object while executing literal in same order. So, if thread A acquires lock on Integer
method1() and thread 2 acquires lock on Integer object while object , thread B will not proceed until thread A releases
executing method2() both will be waiting for each other to Integer lock, same way thread A will not be blocked even if
release lock on Integer and String to proceed further which will thread B holds String lock because now thread B will not
never happen. expect thread A to release Integer lock to proceed further.
Daemon Thread in Java

 The Threads which are executing in the background are called daemon Threads.
 The main objective of daemon Threads is to provide support for non-daemon Threads
like main Thread.
Example:
Garbage collector thread
Whenever the program runs with low memory the JVM will execute Garbage Collector to
provide free memory. So that the main Thread can continue its execution.

 We can check whether the Thread is daemon or not by using isDaemon() method of
Thread class.
public final boolean isDaemon();
 We can change daemon nature of a Thread by using setDaemon () method.
public final void setDaemon(boolean b);
 But we can change daemon nature before starting Thread only. That is after starting
the Thread if we are trying to change the daemon nature we will get R.E saying
IllegalThreadStateException.
 Default Nature: Main Thread is always non daemon and we can't change its
daemon nature because it's already started at the beginning only.
 Main Thread is always non daemon and for the remaining Threads daemon nature
will be inheriting from parent to child that is if the parent is daemon child is also
daemon and if the parent is non daemon then child is also non daemon.
 Whenever the last non daemon Thread terminates automatically all daemon Threads
will be terminated.

Why JVM terminates the daemon thread if there is no user thread?

The sole purpose of the daemon thread is that it provides services to user thread for
background supporting task. If there is no user thread, why should JVM keep running this
thread. That is why JVM terminates the daemon thread if there is no user thread.
Deadlock vs Starvation

 A long waiting of a Thread which never ends is called deadlock.


 A long waiting of a Thread which ends at certain point is called starvation.

A low priority Thread has to wait until completing all high priority Threads (Suppose there are
1 crore of threads out of only one is having priority 1 rest all have max priority i.e. 10 ). This
long waiting of Thread which ends at certain point is called starvation.

How to kill a Thread in the middle of the line?

 We can call stop() method to stop a Thread in the middle then it


will be entered into dead state immediately.
public final void stop();
 stop() method has been deprecated and hence not
recommended to use.

Suspend and resume methods:

 A Thread can suspend another Thread by using suspend()


method then that Thread will be paused temporarily.
 A Thread can resume a suspended Thread by using resume() method then suspended
Thread will continue its execution.
1. public final void suspend();
2. public final void resume();
 Both methods are deprecated and not recommended to use.

GreenThread: The threads which are managed completely by JVM without taking support
for underlying OS, such type of threads are called Green Threads.

Overall Life cycle of thread

You might also like