110 Java Multithreading Interview Questions and Answers (Part 1)

In this post, we have included all the top Java Multi-threading Interview Questions and Answers that are frequently asked in an Interview with detailed answers to it. 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.

1. What is a Thread in Java?
A thread in Java is a lightweight process that runs within another process or thread.
It is an independent path of execution in an application. JVM gives each thread its own  method-call stack.
When we start JVM, Java starts one thread. This thread calls the main method of the class passed in argument to java call.

2. What is the priority of a Thread and how it is used in scheduling?
In Java, every Thread has a priority. This priority is specified as a number between 1 to 10.
Scheduler in Java schedules different threads based on the priority of a thread. It is also known as pre-emptive scheduling.
The thread with higher priority gets preference in execution over a thread with lower priority.

3. What is the default priority of a thread in Java?
In Java, a new thread gets the same priority as the priority of the parent thread that creates it.
Default priority of a thread is 5 (NORM_PRIORITY).

4. What are the three different priorities that can be set on a Thread in Java?
We can set following three priorities on a Thread object in Java:

  • 1. MIN_PRIORITY: This is the minimum priority that a thread can have.
  • 2. NORM_PRIORITY: This is the default priority that is assigned to a thread.
  • 3. MAX_PRIORITY: This is the maximum priority that a thread can have.

Default priority of a thread is 5 NORM_PRIORITY. The value of MIN_PRIORITY is 1 and the value of MAX_PRIORITY is 10.

5. What is the purpose of join() method in Thread class?
In Java, Thread Scheduler controls thread scheduling. But we can use join() method on a thread to make current thread to wait for another thread to finish.
When we use join(), the current thread stops executing. It wait for the thread on which join() is called to finish.
This makes sure that current thread will continue only after the thread it joined finished running. Consider following example:

In the above example, main thread is executing. On Line 1, a new thread called importantThread is ready to run. But at Line 2, mainthread joins the importantThread. Now it lets importantTread to finish and then it moves to Line 3. So currentThread at Line 3 will not start till the importantThread has finished.

6. What is the fundamental difference between wait() and sleep() methods?
The main difference between wait() and sleep() is that wait is an Object level method, whereas sleep() is a static method in Thread class. A waiting thread can be woken up by another thread by calling notify() on the monitor which is being waited on. But a sleeping thread cannot be woken up.
A wait() and notify() has to happen within the same block that is synchronized on the monitor object.
When we call wait() the current thread releases the monitor and goes to waiting state. Then another thread calls notify() to wake it up.
In case of sleep() current thread does not release the monitor or locks. It just sleeps for some pre-defined time period.

7.Is it possible to call run() method instead of start() on a thread in Java?
Yes. We can call run() method of a thread. But it does not work as a separate thread. It will just work as a normal object in main thread and there will not be context switching between the threads.

8. How Multi-threading works in Java?
Java provides support for Multithreading. In a Multithreading environment, one process can execute multiple threads in parallel at the same time.
In Java, you can create process and then create multiple threads from that process. Each process can execute in parallel to perform independent tasks.
Java provides methods like- start(), notify(), wait(), sleep() etc. to maintain a multi-threading environment.

9. What are the advantages of Multithreading?
Main advantages of Multithreading are:

  • 1. Improved performance: We can improve performance of a job by Multi-threading.
  • 2. Simultaneous access to Multiple Applications: We can access multiple applications from a process by doing multithreading
  • 3. Reduced number of Servers required: With Multithreading we need lesser number of servers, since one process can spawn multiple threads.
  • 4. Simplified Coding: In certain scenarios, it is easier to code multiple threads than managing it from same thread.

10. What are the disadvantages of Multithreading?
There are certain downsides to Multithreading. These are:

  • 1. Difficult to Debug: Multithreading code is difficult to debug in case of an issue.
  • 2. Difficult to manage concurrency: Due to multiple threads, we may experience different kinds of issues.
  • 3. Difficulty of porting code: It is difficult to convert existing single threaded code into multi-threading code.
  • 4. Deadlocks: In case of multi-threading we can experience deadlocks in threads that are waiting for same resource.

11. What is a Thread in Java?
In Java, a thread is a lightweight process that runs within another process or thread. It is an independent path of execution in an application. Each thread runs in a separate stack frame.
By default Java starts one thread when the main method of a class is called.

12. What is a Thread’s priority and how it is used in scheduling?
In Java, every Thread has a priority. This priority is specified as an integer value. The priority value is used in scheduling to pick up the thread with higher priority for execution. The threads with higher priority get more preference in execution than the threads with lower priority.
The task scheduler schedules the higher priority threads first, followed by the lower priority threads.

13. What are the differences between Pre-emptive Scheduling Scheduler and Time Slicing Scheduler?
In Pre-emptive scheduling, the highest priority task will keep getting time to execute until it goes to waiting state or dead state or a task with higher priority comes into queue for scheduling.
In Time slicing scheduling, every task gets a predefined slice of time for execution, and then it goes to the pool of tasks ready for execution. The scheduler picks up the next task for execution, based on priority and various other factors.

14. Is it possible to call run() method instead of start() on a thread in Java​?
Yes. We can call run() method of a thread. But it does not work as a separate thread. It will just work as a normal object in main thread and there will not be context-switching between the threads.

15. How will you make a user thread into daemon thread if it has already started?
No. We cannot make a user thread to daemon thread once it has already started.
If we do it by calling setDaemon(), it will throw IllegalThreadStateException

16. Can we start a thread two times in Java?
No. We can call start() method only once on a thread in Java. If we call it twice, it will give us exception.

17.In what scenarios can we interrupt a thread?
We can interrupt a thread if we want to wake it up from the sleep or wait state.

18. In Java, is it possible to lock an object for exclusive use by a thread?
Yes. We can use synchronized block to lock an object. The locked object is inaccessible to any other thread. Only the thread that has locked it can access it.

19. How notify() method is different from notifyAll() method?
In Java, notify() method is used to unblock a specific thread that is in waiting stated. Whereas, notifyAll() method is used to unblock all the threads that are in waiting state.

20. What is a daemon thread in Java?
A daemon thread in Java is a low priority thread that does not prevent the JVM from exiting when the program finishes. The thread keeps running. Garbage Collection is an example of daemon thread.

21. How can we make a regular thread Daemon thread in Java?
We can call setDaemon(boolean) method to change a thread to daemon thread before the thread starts.

22. How will you make a user thread into daemon thread if it has already started?
No. We cannot make a user thread to daemon thread once it has already started. If we do it by calling setDaemon(), it will throw IllegalThreadStateException

23. Can we start a thread two times in Java?
No. We can call start() method only once on a thread in Java. If we call it twice, it will give us exception.

24. What is a Shutdown hook in Java?
The shutdown hook is a thread that is invoked implicitly by JVM just before the shut down. It can be used to clean up unused resources etc.
We can use java.lang.Runtime.addShutdownHook(Thread hook) method to register a new virtual-machine shutdown hook.

25. What is synchronization in Java?
The concept of Synchronization in Java is used in Multi-threading programming.
It is a feature in Java that helps in controlling the access of multiple threads to a shared resource.
It is used to prevent Deadlock between multiple threads.

26. What is the purpose of Synchronized block in Java?
Synchronized block has many uses in Java multi-threading environment. Some of the uses are:

  • It can prevent thread interference
  • It is also used to avoid memory inconsistency issues
  • In general, scope of synchronized block is smaller than the scope of a method.

27.What is static synchronization?
We can make a static method as synchronized in Java. Adding synchronized keyword to a static method can do this.
In static synchronization, the lock is on class not on object.

28. What is a Deadlock situation?
A Deadlock is a situation in which two or more threads are waiting on each other to release a resource. Each thread is waiting for a resource that is held by the other waiting thread.
At times there is a circular wait when more than two threads are waiting on each other’s resources.

29. What is the meaning of concurrency?
Concurrency is the ability of a program to execute several programs simultaneously. This is achieved by distributing computations over multiple CPU cores of a machine or even over different machines
within the same network.
It can increase the speed of execution of the overall program in multi-processor or multi-core system.

30. What is the main difference between process and thread?
As such both process and thread are independent sequences of execution.
The main difference is that a thread runs in a shared memory space, where as a process runs in its own memory space.
A process runs the execution in an environment provided by the operating system. A process has its own set of private resources (e.g. memory, open files, etc.).
A thread lives within a process and shares the resources likememory, open files etc. with the other threads of the same process.
This ability to share resources between different threads makes thread more suitable for tasks where  performance is a significant factor.

31. What is a process and thread in the context of Java?
In Java, a process refers to the running of Java Virtual Machine (JVM). But a thread lives within a JVM and it can be created or stopped by the Java application at runtime.

Continue to reading 100 Java Multithreading Interview Questions and Answers (Part 2)

Leave a Comment

Please share it if you found this useful
Hide Buttons