Wednesday, November 2, 2016

Java MultiThreading Interview Questions

Multi Threading in java is a process of executing multiple threads simultaneously. But we use MultiThreading 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. I have already posted Top 10 Multi Threading Tricky Interview Questions in java

Now, we will learn few more Multi Threading interview questions.

1. 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)

2. 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..

3. 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.


4. 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.

5. 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.

6.  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.

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

8. 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

9. 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.

10. 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.


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...