Wednesday, 3 May 2017

Frequntly asked Interview questions related to Multi Threading in Java

                       I believe, Multi Threading is always an interesting topic. Here, it is the list of frequently asked interview questions related to it. 

1.) What is the difference between Process and thread? 
- How thread switching is good than process switching?
- Life cycle of thread.
- Advantages of multi threading.
- What is difference between user Thread and daemon Thread?

2.) How can we create thread in Java? 
- What is the better way? Extending Thread class or to implement Runnable interface?
- How to create Demon thread in Java?

3.) Difference between start() and run()
- What will happen if we don’t override Thread class run() method?
- Can we call run() without start()? What will happen at that time?
- Can we call run() method of a Thread class?

4.) What is Thread.join() ?
- What do you understand about Thread Priority?
- There are three threads T1, T2, and T3? How do you ensure sequence T1, T2, T3 died before main thread in Java?
- How can we make sure main() is the last thread to finish in Java Program?

5.) wait() vs notify() vs notifyAll() 
- Why they are part of Object class but not Thread class?
- Why these methods have to be called from synchronized method or block?
- What is the difference between calling wait() and sleep() method in Java multi-threading?

6.) What is the difference between Thread.sleep() and Thread.yield() ?
- Why Thread sleep() and yield() methods are static?

7.) What is the difference between the intrupt(), interrupted() and isInterrupted() method in Java?

8.)  What is the difference between Runnable and Collable in Java?
- What is FutureTask in Java?

9.) How to stop a thread in java?
- Why stop(), suspend() and resume() were deprecated.?

10.) What happens when an Exception occurs in a thread? 
- What happens if a thread throws an Exception inside synchronized block?

11.) What is race condition? Give an example.
- What is the solution for that?

12.) What is busy spin in multi-threading?

13.) How can we achieve thread safety in Java?
- Which is more preferred – Synchronized method or Synchronized block?

14.) What is ThreadLocal?
- What is Thread Group? Why it’s advised not to use it?

15.) What is Thread Pool? 
- How can we create Thread Pool in Java?
- What is the difference between the submit() and execute() method thread pool in Java? 
- What happens if you submit a task when the queue of the thread pool is already filled?

16.) What is Thread Scheduler and Time Slicing?
- How can we pause the execution of a Thread for specific time?

17.) How does thread communicate with each other?

18.) How do you share data between two thread in Java?

19.) What is volatile keyword in Java?
- What is the difference between the volatile and atomic variable in Java?

20.) What is Java Thread Dump. How can we get Java Thread dump of a Program?
- Which JVM parameter is used to control stack size of a thread?

21.) What is Java Memory model?

22.) How do you check if a Thread holds a lock or not?

23.) What is blocking method in Java?

24.) What is dead lock? 
- What is Live lock?
- How can we detect that its a dead lock? 
- How to break deadlock? 
- What are the situations which we need to take care so that it can not occur? 
- A sample program which creates a deadlock as well as Live lock.

25.) What is Semaphore in Java? 

26.)  What is the difference between CyclicBarrier and CountDownLatch in Java?

27.) What is ReadWriteLock in Java?

28.) What is the difference between synchronized and ReentrantLock in Java?

29.) Write code to solve Producer Consumer problem in Java? 

30.) What is the fork-join framework in Java? 
- What are new things in JDK 7 related to multi threading?

31.) List down 3 multi-threading best practice you follow?
Answers: 
- Always give meaningful name to your thread.
- Avoid locking or Reduce scope of Synchronization
- Prefer Synchronizers over wait and notify
- Prefer Concurrent Collection over Synchronized Collection