Multithreading Interview Questions

1 - What is Multithreading?
In multithreading, programs may contains more than one part that can run concurrently and each part is known as thread. Multithreading enables to write efficient program that make maximum use of CPU, because idle time is kept minimum.
2 - What is difference between Multiprocessing and Multithreading?
In Multitasking operating system tasks are know as heavy-weight process. In Multithreading environment tasks are light weight process or threads.
Multitasking process takes a seperate address space. These are the different programs run on a single system. Threads on other hand share the same address space and co-operatively share the same heavy weight process.
Inter-process communication is expensive and limited. Inter-thread communication is very light-weight and pervasive.
Context switching, changing the running process, is also heavy-weight. Context switching is fast and an integral part of running any one program.

3 - What is difference between Process and thread?
  • Both process and Thread are independent path of execution but one process can have multiple Threads.
  • Every process has its own memory space, executable code and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and share it with other threads.
  • Threads are also refereed as task or light weight process.
  • Threads from same process can communicate with each other by using keyword like wait and notify and much simpler than inter process communication.
  • Another difference between Process and Thread is that it's How Thread and process are created. It's easy to create Thread as compared to Process which requires duplication of parent process.
  • All Threads which is part of same process share system resource like file descriptors , Heap Memory and other resource but each Thread has its own Exception handler and own stack.
  • Every thread in Java is created and controlled by a unique object of the java.lang.Thread class.

4 - What are different ways of creating thread?
There are two ways of creating Threads
  • Implementing Runnable interface.
  • Extending Thread class.

5 - What is the difference between yield and sleeping?
yield() - yield method pauses the currently executing thread temporarily for giving a chance to the remaining waiting threads of the same priority to execute.

sleep() - It causes the current thread to suspend execution for a specified period. When a thread goes into sleep state it doesn't release the lock
6 - What is the difference between the methods sleep() and wait()?
sleep() - It causes the current thread to suspend execution for a specified period. When a thread goes into sleep state it doesn't release the lock

wait() - - It causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
7 - What is a ThreadLocal class?
ThreadLocal is another way to make thread-safety in java. it eliminates sharing by providing explicitly copy of Object to each thread. Since Object is no more shared there is no requirement of Synchronization which can improve scalability and performance of application. You can use ThreadLocal to make non thread safe object a thread safe object.
8 - What is a daemon thread?
Daemon thread in Java are those thread which runs in background and mostly created by JVM for performing background task like Garbage collection and other house keeping tasks. Thread.setDaemon(true) makes a thread daemon but it can only be called before starting Thread in Java.
9 - What does join( ) method do?
Join method in Java has two variants one which has no argument and other which takes time in millisecond.
  • join()
  • join(long millisec)
A call to any of these two methods invoked on a thread will wait and not return until either the thread has completed or it is timed out after the specified time, respectively.
10 - Why wait() and notify() methods are defined in the Object class, and not in the Thread class?
The wait (), notify () and notify all () methods are object-specific. The wait() method suspends the current thread of execution, The notify()method tells the object to wake up the suspended threads that it is currently keeping track of. Since wait(), notify() and notifyAll() are object specific, they must be used within code that is synchronized on the object. The another reason to define these method in Object class is that, locks are made available on per Object basis.
11 - What is difference between Preemptive scheduling vs. time slicing?
  • Preemptive scheduling means that higher priority task executes until it enter into waiting and dead state or higher priority task come in existence.
  • Time slicing means task executes for a defined slice/ period of time and then enter in the pool of ready state. The scheduler then determine which task execute next based on priority or other factor.

12 - What is the difference between synchronized method and synchronized block?
Synchronized method - If the methods of an object should only be executed by one thread at a time, then the declaration of all such methods should be specified with the keyword synchronized. A thread wishing to execute a synchronized method must first obtain the object's lock (i.e., hold the lock) before it can enter the object to execute the method. This is simply achieved by calling the method. If the lock is already held by another thread, the calling thread waits.

Synchronized block - It allows execution of arbitrary code to be synchronized on the lock of an arbitrary object. The general form of the synchronized statement is as follows:
synchronized (<object reference expression>) { <code block> }
The <object reference expression> must evaluate to a non-null reference value, otherwise, a NullPointerException is thrown. The code block is usually related to the object on which the synchronization is being done.
13 - Can we call run() method of a Thread class?
Yes, we can call run() method of a Thread class but then it will behave like a normal method. To actually execute it in a Thread, you should call Thread.start() method to start it.
14 - What is Starvation?
Starvation is a situation where a thread is unable to get continuous access to shared resources and unable to make progress. This happen when some another thread use shared resource for longer time.