Sunday, January 29, 2017

Java MultiThreading Interview Questions

Multi Threading in java is a process of executing multiple threads simultaneously. But we use Multi Threading than multi processing because threads share a common memory area. In this post,we will learn multi threading interview questions and answers. These interview questions are frequently asked by interviewer.

1. What is Thread in java?

Ans: Thread is an Independent sequential path of execution within a process. Threads are light weight process. Threads are exists in common memory space and can share resources data and code.Threads can run parallel. In java, Thread  is represented and controlled by an object of class Java.lang.Thread.


2. What is difference between Thread and Process?

Ans:  A Thread is lightweight compared to process.Threads are subdivision of process.Threads exists within a process and shares the process resources like memory whereas Process is self contained execution environment. Each process has its own memory space.
Threads have direct access  to the data segment of its process whereas process  has own copy of the data segment.
Context-switching between threads in the same process is very fast whereas context-switching between process is slower.
Threads can easily communicate within the process whereas  process communicate only system provided inter process communication mechanism.

3. How many ways you can create Thread in java?

Ans:  You can create a Thread in two ways in java. They are

    a) Extending a Thread class

    b)  Implementing a Runnable interface

4. What is Multitasking? How it can be achieved?

Ans: Multitasking is a process of executing multiple tasks simultaneously. We use multitasking to utilize the CPU power. 

Multitasking can be achieved by two ways: 

1. Process-based Multitasking(Multiprocessing)
2.Thread-based Multitasking(Multithreading)

5. What is Multiprocessing ?

Ans: Multiprocessing is nothing but process-based Multitasking. Each process have its own address in memory i.e. each process allocates separate memory area.Process is heavy weight. Cost of communication between the process is high. Switching from one process to another require some time for saving and loading registers,memory maps,updating lists etc..

6. What are the benefits of Multithreaded Programming?

Ans: In multi-threaded programming,multiple threads are executing concurrently that improves the performance because CPU is not idle in case some thread is waiting to get some resources. Multiple threads share the heap memory. So, it is good to create multiple threads to execute some task rather than creating multiple processing.


Example: Servlets are better in performance than CGI because servlet support multi-threading but CGI does not. It does not block the user because threads are independent and you can perform multiple operations at same time. Threads are independent so it doesn't affect other threads if exception occur in a single thread.

7. Difference between user Thread and daemon Thread?

Ans: When  we create a Thread in java program,it's known as user Thread. A daemon thread runs in background and doesn't prevent JVM from terminating. When there are no user threads running. JVM shutdown the program and quits.



8. How can we pause the execution of a Thread for specific time?

Ans: we can use Thread class sleep() method to pause the execution of Thread for certain time. Note that this will not stop the processing of thread for specific time,once the thread awake from sleep,it's state get changed to runnable and based on thread scheduling,it gets executed.

9.  What is Context-switching in multi-threading?

Ans: Context-switching is the process of storing and restoring of CPU state so that Thread execution can be resumed from the same point at a later point of time.It is essential feature for multitasking operating system and support for multi-threaded environment.

10.  How can you make sure main() is the last thread to finish in java program?

Ans: we can use Thread join() method to make sure all the threads created by the program is dead before finishing the main function.

11. Why thread communication methods wait(),notify(),notifyAll() are in Object class?

Ans: In java every object has a monitor and wait,notify methods are used to wait for the Object monitor or to notify other threads that Object monitor is free now. There is no monitor on threads in java and synchronization can be used with any Object,that's why it's part of Object class so that every class in java has these essential methods for inter thread communication

12What is Difference between Wait() and sleep() in java?


Ans:  Firstly you must aware of where these methods are exists. wait() method exist in Object class whereas sleep() method available in the Thread class. sleep() method is used the currently executing thread to sleep for specific period of time(in milliseconds)  whereas wait() method causes the current thread to wait until another thread invokes notify() or notifyall() for this object. Here thread releases the lock as soon as wait is called,but in case of sleep() method the Thread does not release the lock.

13. How can we achieve thread safety in Java?

Ans: There are several ways to achieve thread safety in java-Synchronization,atomic concurrent classes,implementing concurrent Lock interface,using volatile keyword,using immutable classes and Thread safe classes.

14. Which is more preferred Synchronized method or Synchronized block?

Ans: Synchronized block is more preferred way because it does not lock the Object,synchronized methods lock the Object and if there are multiple synchronization blocks in the class,even  though they are not related,it will stop them from execution and put them in wait state to get the lock on Object.


15) What is the use of Synchronized in java?

Ans: Synchronized keyword is used  in java to control the access of multiple threads on shared resources. Synchronized keyword can be applied to static and instance methods and synchronized block in java of  the code. If you apply synchronized keyword for method only one thread can access the same thread then other threads have to wait for the execution of method by one thread. Synchronized keyword provides a lock on the object.

16)  What is the internal process when start() method is called?

Ans:  A new Thread of execution with a new call stack starts. And then the state of thread change from new to Runnable. When  thread gets chance to execute its target run() method start to run.

17) What will happen if you do not override run()method?

Ans:  This is basic question on thread , interviewer want to know your knowledge on thread api and you really have knowledge how to create and run a thread.

When we call start() method it internally calls run() method  with newly created thread.So if we do not override run() method a newly created thread will not called and nothing happen. that run() method executes just like normal method.

18) What is difference between notify() and notifyall() method in java?


Ans: Their might be several threads waiting for this object and only one of them is chosen.notify() method  is main work is wake up a single thread  that is waiting on this object whereas notifyall() methods wakes up all the threads that are waiting on this object's monitor.


No comments:

Post a Comment