Thursday, 15 October 2015

Java MultiThreading Interview Question



I am publishing some interview question which asked to me(and my friends) while I am applying for new change with java profile.

            Right now I am adding only question will add ans in time to time.

 
Multi threading :  

1.   What is context switching in multi-threading?  

Ans . Switching context from one thread to another thread is called context switching. Mostly OS uses round robin preempted mechanism for context switching.
Context switching should not be very frequently, it may degrade performance, because it will waste more time in switching context instead doing actual processing in threads


 2. Difference between deadlock and livelock,  starvation?

DeadLock - It’s situation where one thread waiting for a resource that is locked by other resources.

Livelock -  Scenario is same like deadlock but the basic difference is that in this case both thread pretend to solve deadlock issue . Eg two mens stuck in tunnel in which at one time only one can pass.

Starvation - Starvation is case where one thread get neglected by thread manager 


3. What thread-scheduling algorithm is used in Java?

Java does not its own scheduling algorithm, it uses underlying operating system thread scheduling algorithms. Operating system uses Round Robin scheduling , Preemptive strategy for scheduling threads 


4. What is thread-scheduler in Java?
    Thread scheduler is part of OS which which mainly control thread execution.
    There is no guarantee that which runnable thread will be chosen to run by thread schedule.


 5. How do you handle unhandled exception in thread?

Ans : To handle unhandled exception we need to add own uncaughtExceptionHandler.
If we haven't implemented any handler then it will throw exception like below
                Exception in thread "Thread-0" java.lang.RuntimeException at Main$1.run(Main.java:11) at java.lang.Thread.run(Thread.java:619)

Thread t = new Thread(new Runnable()
 {
   public void run()
    {
      throw new RuntimeException();
 } });
 t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {   
  public void uncaughtException(Thread t, Throwable e)
 {
        System.out.println("exception " + e + " from thread " + t);
} });  
     t.start();


To set handler for all threads use a static method Thread.setDefaultUncaughtExceptionHandler.

6. What is thread-group, why it's advised not to use thread-group in Java? 

Ans. When you want to execute bunch of thread at once rather than individual then Thread group comes handy.
It’s provide a mechanism for collecting multiple threads into a single object .


The runtime system puts a thread into a thread group during thread construction.


-you cannot move a thread to a new group after the thread has been created


public Thread(ThreadGroup group, Runnable target)
public Thread(ThreadGroup group, String name)
public Thread(ThreadGroup group, Runnable target, String name)


Example ::
ThreadGroup myThreadGroup = new ThreadGroup("My Group of Threads");
Thread myThread = new Thread(myThreadGroup, "a thread for my group");

7. Why Executor framework is better than creating and managing thread by application
 
8.  Difference between Executor and Executors in Java?
  
9. How to find which thread is taking maximum cpu in windows and Linux server?.

10. What is ThreadLocal class used for ?
Ans. It’s  alternative way to get ThreadSafety in java. It’s eliminate synchronization requirement by sharing explicit copy of object to each thread



11.
How will you design your own custom thread pool in Java ? Don't use the One provided by JDK ?


 Ans. Need to follow below steps. 
 
1)-  Take variable blockingQueue (To save task).
2)- variable like list of type thread
3)- Boolean to check status of thread
4)- Create parameterized constructor   with value of task and thread
5)- Create Enque and Deque Method for same
 

12. Why wait, sleep, notify and notifyAll methods are in Object class not in Thread class?

13. Implement runnable functionality for your own.
14. If any thread have class level locking as well as instance level locking then both are mutually exclusive or not ?

15 what is difference between yield() and joins().

16. What are the thread state.

17. Best way to handle exception thrown by Callable in multi threading?
18. Difference between Countdown Latch and Countdown Barrier.  

19.Sleep VS wait?
  
20. How do you make a class synchronized ?

21. Best way to handle exception using callable? 

22. Implement BlockingQueue to solve producer consumer problem ?

23. Implementation of countDownLatch?

  
 

1 comment: