Friday, January 29, 2016

Java Multithreading Interview Questions

1.How can we create a Thread in Java?
Ans: There are two ways to create Thread in Java – first by implementing Runnable interface and then creating a Thread object from it and second is to extend the Thread Class.
2. Can a thread call a non-synchronized instance method of an Object when a synchronized method is being executed ?
Ans: Yes, a Non synchronized method can always be called without any problem. In fact Java does not do any check for a non-synchronized method. The Lock object check is performed only for synchronized methods/blocks.
3. Can two threads call two different synchronized instance methods of an Object?
Ans: No. If a object has synchronized instance methods then the Object itself is used a lock object for controlling the synchronization. Therefore all other instance methods need to wait until previous method call is completed.
4. Can we synchronize the run method? If yes then what will be the behavior?
Ans: Yes, the run method of a runnable class can be synchronized. If you make run method synchronized then the lock on runnable object will be occupied before executing the run method. In case we start multiple threads using the same runnable object in the constructor of the Thread then it would work. But until the 1st thread ends the 2nd thread cannot start and until the 2nd thread ends the next cannot start as all the threads depend on lock on same object.
5.Can we call run() method of a Thread class?
Ans: 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, we need to start it using Thread.start() method.
6. What is thread pool? Why should we use thread pools?
Ans: A thread pool is a collection of threads on which task can be scheduled. Instead of creating a new thread for each task, you can have one of the threads from the thread pool pulled out of the pool and assigned to the task. When the thread is finished with the task, it adds itself back to the pool and waits for another assignment.
7. Can we synchronize the constructor of a Java Class?
Ans: As per Java Language Specification, constructors cannot be synchronized because other threads cannot see the object being created before the thread creating it has finished it.
8. What is a volatile keyword?
Ans: The copy of volatile variable is stored in the main memory, so every time a thread access the variable even for reading purpose the local copy is updated each time from the main memory. The volatile variable also have performance issues.
10. What is the difference between wait() and sleep()?
Ans:
1) The wait() method is defined in Object class. The sleep() method is defined in Thread class.
2) The wait() method releases the lock. The sleep() method does not release the lock.
3) The wait() method should be called from synchronized method or block. There is no such requirement for sleep() method.
4) The wait() method is an instance specific method and only got wake up if some other thread calls notify or notifyAll methods on same object.The sleep() method is a static method and applies on current thread.
11. What is the difference between yield() and sleep()?
Ans: yield() allows the current the thread to release its lock from the object and scheduler gives the lock of the object to the other thread with same priority.
sleep() allows the thread to go to sleep state for x milliseconds. When a thread goes into sleep state it doesn’t release the lock.
12. How many locks does an object have?
Ans: Each object has only one lock.
13. What does join() method do?
Ans: t.join( ) allows the current thread to wait indefinitely until thread “t” is finished. t.join (5000) allows the current thread to wait for thread “t” to finish but does not wait longer than 5 seconds. Note that join() can also take times in milliseconds and nanoseconds as parameters. These values specify the maximum amount of time to wait before returning the blocked thread’s state to RUNNABLE.
14. The following statements are true.
1. wait( ), notify( ) ,notifyall( ) are defined as final & can be called only from within a synchronized method
2. Among wait( ), notify( ), notifyall( ) the wait() method only throws IOException
15. You have thread T1, T2 and T3, how will you ensure that thread T2 run after T1 and thread T3 run after T2?
Ans: It can be achieved by using join method of Thread class.
16. Why we call start() method which in turns calls run() method, why not we directly call run() method ?
Ans: when you call start() method it creates new Thread and execute code declared in run() while directly calling run() method doesn’t create any new thread and execute code on same calling thread.
17. What is Java Thread Dump, How can we get Java Thread dump of a Program?
Ans: Thread dump is list of all the threads active in the JVM, thread dumps are very helpful in analyzing bottlenecks in the application and analyzing deadlock situations. There are many ways using which we can generate Thread dump – Using Profiler, Kill -3 command, jstack tool etc.
18. What is ReentrantLock? How it is different from synchronized?
Ans: The ReentrantLock has the same concurrency and memory semantics as synchronized. It also provides features like lock polling, timed lock waits, and interruptible lock waits. Additionally, it offers far better performance under heavy contention.
Protecting a block of code with ReentrantLock:
1
2
3
4
5
6
7
8
9
Lock lock = new ReentrantLock();
 
lock.lock();
try {
// update object state
}
finally {
lock.unlock();
}
We can use the ReentrantLock when we actually need something it provides that synchronized doesn’t, like timed lock waits, interruptible lock waits etc. It also has scalability benefits, and you should use it if you actually have a situation that exhibits high contention. However, the existences of these obvious benefits are not a good enough reason to always prefer ReentrantLock over synchronized.
In majority of the cases, synchronized works just fine, works on all JVMs, is understood by a wider range of developers, and is less error-prone. So make the decision on the basis of need.
19. How to Stop a Thread in java?
Ans: We can use Thread.interrupt() to stop a Thread.
20. Difference between run() and start() methods of Thread?
Ans: The start() method creates a new thread and then execute the run() method.
The run() method just executes in the current thread, without starting a new thread.

No comments:

Post a Comment