Sunday, 5 March 2017

Multithreading interview questions


Thread
Independent path of execution

Thread vs. Process
One process can contain multiple threads

How to implement thread ?
Thread class and Runnable interface   (Thread implements Runnable)
Runnable is better which provides opportunity to extend another class.



Use of Volatile
Every Thread has its own local memory for its instance variables.
We may alter the value of instance variable from any thread.
M
aking a variable volatile guarantees that the thread will also see any update done before initializing volatile variable.



start() vs. run()
Invoking run() will execute method but will not start a thread.

How to stop the thread ?
Thread will only stop once run method is finished and cannot be restarted once finished.
You can check a boolean instance variable in the loop inside your class extending Thread.
Example
private class MyThread extends Thread {
    boolean tExit = false;
 
    public void exit(boolean tExit) {
        this.tExit = tExit;
    }
 

    @Override
    public void run() {
        while(!tExit){
            // Your logic here...
        }
    }
}

Why should we make tExit variable volatile in above example ?
It is a good practice to make tExit volatile because any thread may alter the value of tExit.
Making it volatile guarantees that MyThread will also see any update done before initializing tExit.
Runnable vs. Callable
Callable's call() method can :

  • Return value (A Future object which can hold the result of computation)
  • Throw Exception

CyclicBarrier vs. CountDownLatch
Both waits for wait for number of threads on one or more events
We cannot re-use CountDownLatch once count reaches to zero, but you can reuse same CyclicBarrier even after barrier is broken



notify vs. notifyAll
notify() doesn't provide any way to choose a particular thread – useful when there is only one thread waiting
notifyAll() sends notification to all threads and allows them to compete for locks

Why wait, notify and notifyAll are not inside thread class ?

ThreadLocal variable
ThreadLocal variable is per thread. Stores local state of each thrad.

FutureTask
Base impl of Future interface
Have methods to :
·         methods to start and cancel a computation
·         query to see if the computation is complete
·         retrieve the result of the computation
interrupted() vs. isInterrupted()
interrupted is :
·         static method
·         clears the interrupt status (An internal flag)

How you can ensure the sequence of 3 threads ?
Use join()
Start from T3 , call join() so that it calls T2 , inside T2 call join() again so that it calls T1
By this way, the execution sequence would be T1 -> T2 -> T3

Concurrency level of ConcurrentHashMap
Partitions actual map into a number of sections (# sections = Concurrency level) , Default = 16 , Permits concurrent updates
What is Semaphore ?
It maintains a set of permits
acquire() blocks until a permit is available

No comments:

Post a Comment

Note: only a member of this blog may post a comment.