A Java developer must know multi-threaded programming spring interview questions and answers pdf java. A good hands on experience is required to understand the core threads concepts.
Multi-threading and thread optimization is a common topic in java performance tuning books as well. This post is a collection of some good questions on Java threads, which can be asked to a senior java developer. What is synchronization in respect to multi-threading in Java? With respect to multi-threading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one Java thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to erroneous behavior or program. Explain different way of using thread?
A Java thread could be implemented by using Runnable interface or by extending the Thread class. The Runnable is more advantageous when you are going for multiple inheritance. What is the difference between Thread. Thread class actually does the job of running the Thread.
Can we achieve it with only run method? JVM needs to create a separate thread which can not be differentiated from a normal method call. So this job is done by start method native implementation which has to be explicitly called. Another advantage of having these two methods is we can have any object run as a thread if it implements Runnable interface.
This is to avoid Java’s multiple inheritance problems which will make it difficult to inherit another class with Thread. A thread-local variable effectively provides a separate copy of its value for each thread that uses it. Common use is seen in DAO pattern where the DAO class can be a singleton but the Database connection can be maintained separately for each thread. Object from a point in your program where u are NOT having a lock on that object. Not Runnable” state for specified amount of time. The thread holds the monitors it has acquired. A common mistake is trying to call t2.
Its possible to send other threads into suspended state by making a suspend method call. In suspended state, a thread keeps all its monitors and can not be interrupted. This may cause deadlocks, therefore, it has been deprecated. Another thread T2 comes to execute the synchronized block and find that it’s already acquired.
T1 thread finishes all its synchronized block work. Since the T2 thread is first in the queue of waiting it acquires the lock and starts processing. What happens when I make a static method as synchronized? 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. In case the method is not declared synchronized Jave will call even if you are playing with shared data. So you have to be careful while doing such thing. The decision of declaring a method as synchronized has to be based on critical section access. Can two threads call two different synchronized instance methods of an Object?
If an 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. See the below sample code which demonstrates it very clearly. Deadlock is a situation where two or more threads are blocked forever, waiting for each other. This may occur when two threads, each having a lock on one resource, attempt to acquire a lock on the other’s resource.