Q-1: What is the thread?
A thread is a
lightweight subprocess. It is a separate path of execution because each thread
runs in a different stack frame. A process may contain multiple threads.
Threads share the process resources, but still, they execute independently.
Q-2: What is multithreading?
Multithreading is a
process of executing multiple threads simultaneously. Multithreading is used to
obtain the multitasking.
Q-3: Differentiate between process and thread?
There are the
following differences between the process and thread.
- Program in the execution
is called the process whereas; A thread is a subset of the process
- Processes are
independent whereas threads are the subset of process.
- Process have different
address space in memory, while threads contain a shared address space.
- Context switching is
faster between the threads as compared to processes.
- Inter-process
communication is slower and expensive than inter-thread communication.
- Any change in Parent
process doesn't affect the child process whereas changes in parent thread
can affect the child thread.
Q-4: What are the advantages of multithreading?
Multithreading
programming has the following advantages:
- Multithreading allows an
application/program to be always reactive for input, even already running
with some background tasks
- Multithreading allows the faster
execution of tasks, as threads execute independently.
- Multithreading provides better
utilization of cache memory as threads share the common memory resources.
- Multithreading reduces the number of the
required server as one server can execute multiple threads at a time.
Q-5: What are the states in the lifecycle of a Thread?
A thread can have one
of the following states during its lifetime:
- New: In this state, a Thread class object is created using
a new operator, but the thread is not alive. Thread doesn't start until we
call the start() method.
- Runnable: In this state, the thread is ready
to run after calling the start() method. However, the thread is not yet
selected by the thread scheduler.
- Running: In this state, the thread scheduler
picks the thread from the ready state, and the thread is running.
- Waiting/Blocked: In this state, a thread is not
running but still alive, or it is waiting for the other thread to finish.
- Dead/Terminated: A thread is in terminated or dead
state when the run() method exits.
Q-6: What is context switching?
In Context switching
the state of the process (or thread) is stored so that it can be restored and
execution can be resumed from the same point later. Context switching enables
the multiple processes to share the same CPU.
Q-7: Differentiate between the Thread class and Runnable
interface for creating a Thread?
The primary
differences between both the ways are given below:
- By extending the Thread class, we cannot
extend any other class, as Java does not allow multiple inheritances while
implementing the Runnable interface; we can also extend other base
class(if required).
- By extending the Thread class, each of
thread creates the unique object and associates with it while implementing
the Runnable interface; multiple threads share the same object
- Thread class provides various inbuilt
methods such as getPriority(), isAlive and many more while the Runnable
interface provides a single method, i.e., run().
Q-8: What is the synchronization?
Synchronization is the
capability to control the access of multiple threads to any shared resource. It
is used:
When the multiple
threads try to do the same task, there is a possibility of an
erroneous result, hence to remove this issue, Java uses the process of
synchronization which allows only one thread to be executed at a time.
Synchronization can be achieved in three ways:
- by the synchronized method
- by synchronized block
- by static synchronization
Q-9: What is the purpose of the Synchronized block?
The Synchronized block can be used to perform synchronization on any specific resource of the method. Only one thread at a time can execute on a particular resource, and all other threads which attempt to enter the synchronized block are blocked.
Q-10: What is the difference between notify ()
and notifyAll ()?
The notify () is used to unblock one
waiting thread whereas notifyAll () method is used to unblock all the threads
in waiting state.
Q-11: What about the daemon threads?
The daemon threads are the low priority
threads that provide the background support and services to the user threads.
Daemon thread gets automatically terminated by the JVM if the program remains
with the daemon thread only, and all other user threads are ended/died.
Q-12: What is the difference between wait ()
and sleep () method?
wait() |
sleep () |
1)
The wait() method is defined in Object class. |
The
sleep () method is defined in Thread class. |
2)
The wait() method releases the lock. |
The
sleep () method doesn't release the lock. |
Q-13: What does join() method?
The join() method waits for a thread to
die. In other words, it causes the currently running threads to stop executing
until the thread it joins with completes its task.
Q-14: What do you understand by inter-thread communication?
- The process of communication between
synchronized threads is termed as inter-thread communication.
- Inter-thread communication is used to
avoid thread polling in Java.
- The thread is paused running in its
critical section, and another thread is allowed to enter (or lock) in the
same critical section to be executed.
- It can be obtained by wait(), notify(),
and notifyAll() methods.
👉Java Exception handling Interview Questions and Answers
👉Oops Interview Questions in Java for Freshers
👉Java Collection Framework Interview questions for Freshers
👉Java9 Interview Questions & Answers for beginners
👉Java8 Interview Questions & Answers for beginners
0 Comments