Introduction to Java Multi-threading Interview Questions And Answers
Java is on boom these days due to its independent platform. It can be run anywhere and hence is being used widely with different upcoming technologies. To be prepared for it, multi-threading is an important topic that is focused on interviews. So you have finally found your dream job in Java Multi-threading but are wondering how to crack the interview and what could be the probable Java Multi-threading Interview Questions. Every interview is different and the scope of a job is different too. Let us go through a few important Java Multi-threading Interview Questions which may be asked in the Java Multi-threading interviews that you would pursue.
Part 1 – Java Multi-threading Interview Questions (Basic)
This first part covers basic Interview Questions and Answers
1. What is Thread in JAVA and how do you implement it?
Answer:
A thread helps facilitates multi-tasking and multi-processing within a program. It utilizes CPU effectively and helps improve the performance of the application. Java enables the user to use multiple threads at a time. It can run in parallel with other programs without any interruptions. This helps in reducing the time for the process. For example, if a program takes 60 seconds to execute, we can use 6 threads which will give us the result in 10 seconds.
Java provides two ways to implement threads in a program. interface java.lang.Runnable has an instance java.lang.A thread that requires a task to execute through an instance. The Thread class already implements Runnable, so a user can directly override run() method by extending Thread class or by implementing the Runnable interface.
2.When to use Runnable and when to use Thread in Java?
Answer:
Java does not support multiple inheritances of a class, but it allows us to implement multiple interfaces at a time. Therefore, it will be better to implement Runnable than extending the Thread class.
3. How do you differentiate between a thread and a process?
Answer:
A thread is a subset of a process. One process can have multiple threads running for it. Threads can run any part of a process. A process has its own address where it is stored, while thread shares the address of the process that has created it. Creating a process is a hectic process while threads can be created easily. A thread is usually referred to as a lightweight process. Interprocess communication is difficult while threads can communicate easily using the Java methods of wait() and notify(). Processes are independent, hence any change made to process does not affect child processes. On the contrary, if changes are made in a thread there are possibilities that other threads may be affected.
4. Why is it said that Thread’s behavior is unpredictable?
Answer:
The reason for this is the thread scheduler which handles the execution of threads. The scheduler may be having different performances on different platforms Windows, UNIX, LINUX, etc. While executing the same thread may give different outputs on different platforms and sometimes even on the same platform. To solve this, a user can create the same Runnable object, create run() loops in both threads and start both threads together. Ideally, the scheduler should pick both threads together and the performance should not be affected, but both threads will enter loops anonymously.
4.8 (8,018 ratings)
View Course
5. What is a volatile variable in Java and what is its significance?
Answer:
Java facilitates users to share variables present in different threads. A volatile variable acts as a special modifier that can be used only for instance variables. As this variable is shared, there would be consistent changes with it. The thread must ensure that it has exclusive access to these variables and a lock is compelled for mutual exclusion of shared variables. It ensures that a write will happen prior to any consequent read. The Java memory model ensures the consistency of this variable. Volatile methods cannot be there as they can be only used in the form of variables.
Part 2 – Java Multi-threading Interview Questions (advanced)
This first part covers advanced Interview Questions and Answers
6. What is the use of the synchronized keyword? What is the difference between synchronized and volatile keywords?
Answer:
The synchronized keyword is used when the purpose Is to run only one thread at a time in an appropriate section of code. It can be used to show four types of different blocks as below:
- Instance methods
- Static methods
- Code blocks inside instance methods
- Code blocks inside static methods
It can be declared as:
Public synchronized void example () {}
The difference between volatile and synchronized keyword is that synchronized can be used with variables and methods, while volatile cannot be used with methods. Volatile variables are not stored in the cache memory, synchronized variables are stored in cache memory. A volatile variable will never land up in deadlock as it does not require to obtain any lock. While in synchronized variables if they are not done properly then it may end up in a deadlock.
7. Why methods like wait(), notify(), and notify all() are present in object class and not in Thread class?
Answer:
Object class has monitors that allow the thread to lock an object, while Thread does not have any monitors. When any of the above methods are called it waits for another thread to release the object and notifies the monitor by calling notify() or notify all(). When notify() method is called it does the job of notifying all threads which are waiting for the object to be released. The object class’s monitor checks for the object if it is available or not. Thread class having these methods would not help as multiple threads exist on an object and not vice versa.
8. Explain the difference between sleep() and wait() methods.
Answer:
- sleep() method is called on threads and not objects. Wait() method is called on objects.
- When the wait() method is called then monitor moves the thread from running to waiting state. Once a thread is in wait() then it can move to runnable only when it has notify() or notifyall() for that object. The scheduler changes the state after this. While in the sleep() method, the state is changed to wait and will return to runnable only after sleep time is over.
- Wait() method is a part of java.lang.Object class, while sleep() is a part of java.lang.Thread class.
- Wait() is always used with a synchronized block as it requires to lock an object while sleep() can be used from the outside synchronized block.
9. How to force start a thread in Java?
Answer:
In Java, multithreading one cannot force start a thread. Only thread schedulers can control threads and they are not exposed to any API for control.
10. Does thread leave object lock when wait() and sleep() methods are called?
Answer:
When a thread is in sleep() method it does not leave lock and moves to the waiting state. The thread waits for sleep time to get over. When wait() method is used thread leaves the object lock and goes to waiting state. Once notify() is called it again goes to running state and acquires the lock.
Recommended Article
This has been a guide to List Of Java Multi-threading Interview Questions and Answers so that the candidate can crackdown these Interview Questions easily. You may also look at the following articles to learn more –