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?

  
 

Java Collection and DataStructure Interview Questions







1. What is the strategy to handle ConcurrentModificationException in your Java program.

2. How hashmap identify index for element to insert?

3. What is  default size of bucket in hashmap? and formula of rehash size?
4. Comparable VS Comparator
5. Write Comparator for employee class having id and name?
6. ArrayList VS LinkedList
7. Can we add null to TreeSet? if No why?

8.
   ArrayList<String> s = new ArrayList<String>(); 
   ArrayList<Integer> s1 = new ArrayList<Integer>();

    System.out.println(s1.equals(s));

why ans is always equals
 

9. Implement own linked list.

10. Implement own linked list.

11. What is the difference between HashMap and Synchronized map.

12. how does blocking queue works? 

13. Which collection  should use where you keep elements like list. With out using AL or LL ? 

14. How to write your own doubly link list? How do you handle boundary conditions? 

15. Why java have “for loop”, foreach loop, iterator and ListIterator for traversing ArrayList,
  List out difference between them and give valid reason what you prefer.


Ans
 for loop - Good to traversing Arraylist only, If you are not sure underlying data structure do not use for loop for traversing as for linked list N^2 time complexity
For Each Loop - can call methods of element

Iterator : 
// 1 - can call methods of element // 2 - can use iter.remove() to remove the current element from the list

ListIterator :
// 1 - can call methods of element // 2 - can use iter.remove() to remove the current element from the list // 3 - can use iter.add(...) to insert a new element into the list // between element and iter->next() // 4 - can use iter.set(...) to replace the current element




16. What will be behavior of a Java Program where infinite recursion takes place with limited Stack memory ? Does the program complete or Stackoverflow exception is thrown ?

17. How to handle multiple request using concurrency utilities?