Multithreading Interview Questions In Java

Multithreading Interview Questions: Multithreading is a programming concept that allows multiple threads of execution to run concurrently within a single program. Multithreading has become a vital part of software development with the increasing demand for responsive and efficient applications. As a result, companies often require their developers to have a thorough understanding of multithreading and the ability to write code that takes advantage of its benefits.

To assess a candidate’s expertise in multithreading, interviewers frequently ask various questions related to the topic. These questions can range from fundamental concepts to advanced techniques. Being well-prepared for them can significantly increase a candidate’s chances of landing a job in the software development industry. In this article, we will discuss some of the commonly asked multithreading interview questions and how to answer them.

Multithreading Interview Questions In Java

  • How Multi-threading works in Java?
  • What are the advantages of Multithreading?
  • What are the disadvantages of Multithreading?
  • What is a Thread in Java?
  • What is a Thread’s priority and how it is used in scheduling?
  • What are the differences between Pre-emptive Scheduling Scheduler and Time Slicing Scheduler?
  • Is it possible to call run() method instead of start() on a thread in Java?
  • How will you make a user thread into daemon thread if it has already started?
  • Can we start a thread two times in Java?
  • In what scenarios can we interrupt a thread?
  • In Java, is it possible to lock an object for exclusive use by a thread?
  • How notify() method is different from notifyAll() method?
  • What is a Thread in Java?
  • What is the priority of a Thread and how it is used in scheduling?
  • What is the default priority of a thread in Java?
  • What are the three different priorities that can be set on a Thread in Java?
  • What is the purpose of join() method in Thread class?
  • What is the fundamental difference between wait() and sleep() methods?
  • Is it possible to call run() method instead of start() on a thread in Java?
  • What is a daemon thread in Java?
  • How can we make a regular thread Daemon thread in Java?
  • How will you make a user thread into daemon thread if it has already started?
  • Can we start a thread two times in Java?
  • What is a Shutdown hook in Java?
  • What is synchronization in Java?
  • What is the purpose of Synchronized block in Java?
  • What is static synchronization?
  • What is a Deadlock situation?
  • What is the meaning of concurrency?
  • What is the main difference between process and thread?
  • What is a process and thread in the context of Java?
  • What is a Scheduler?
  • What is the minimum number of Threads in a Java program?
  • What are the properties of a Java thread?
  • What are the different states of a Thread in Java?
  • How will you set the priority of a thread in Java?
  • What is the purpose of Thread Groups in Java?
  • Why we should not stop a thread by calling its stop() method?
  • How will you create a Thread in Java?
  • How can we stop a thread in the middle of execution in Java?
  • How do you access the current thread in a Java program?
  • What is Busy waiting in Multi-threading?
  • How can we prevent busy waiting in Java?
  • Can we use Thread.sleep() method for real-time processing in Java?
  • Can we wake up a thread that has been put to sleep by using Thread.sleep() method?
  • What are the two ways to check if a Thread has been interrupted?
  • How can we make sure that Parent thread waits for termination of Child thread?
  • How will you handle InterruptedException in Java?
  • Which intrinsic lock is acquired by a synchronized method in Java?
  • Can we mark a constructor as synchronized in Java?
  • Can we use primitive values for intrinsic locks?
  • Do we have re-entrant property in intrinsic locks?
  • What is an atomic operation?
  • Can we consider the statement i++ as an atomic operation in Java?
  • What are the Atomic operations in Java?
  • Can you check if following code is thread-safe?
  • What are the minimum requirements for a Deadlock situation in a program?
  • How can we prevent a Deadlock?
  • How can we detect a Deadlock situation?
  • What is a Livelock?
  • What is Thread starvation?
  • How can a synchronized block cause Thread starvation in Java?
  • What is a Race condition?
  • What is a Fair lock in multi-threading?
  • Which two methods of Object class can be used to implement a Producer Consumer scenario?
  • How JVM determines which thread should wake up on notify()?
  • Check if following code is thread-safe for retrieving an inte ger value from a Queue?
  • How can we check if a thread has a monitor lock on a given object?
  • What is the use of yield() method in Thread class?
  • What is an important point to consider while passing an object from one thread to another thread?
  • What are the rules for creating Immutable Objects?
  • What is the use of ThreadLocal class?
  • What are the scenarios suitable for using ThreadLocal class?
  • How will you improve the performance of an application by multithreading?
  • What is scalability in a Software program?
  • How will you calculate the maximum speed up of an application by using multiple processors?
  • What is Lock contention in multi-threading?
  • What are the techniques to reduce Lock contention?
  • What technique can be used in following code to reduce Lock contention?
  • What is Lock splitting technique?
  • Which technique is used in ReadWriteLock class for reducing Lock contention?
  • What is Lock striping?
  • What is a CAS operation?
  • Which Java classes use CAS operation?
  • Is it always possible to improve performance by object pooling in a multi-threading application?
  • How can techniques used for performance improvement in a single thread application may degrade the performance in a multi-threading application?
  • What is the relation between Executor and ExecutorService interface?
  • What will happen on calling submit() method of an ExecutorService instance whose queue is already full?
  • What is a ScheduledExecutorService?
  • How will you create a Thread pool in Java?
  • What is the main difference between Runnable and Callable interface?
  • What are the uses of Future interface in Java?
  • What is the difference in concurrency in HashMap and in Hashtable?
  • How will you create synchronized instance of List or Map Collection?
  • What is a Semaphore in Java?
  • What is a CountDownLatch in Java?
  • What is the difference between CountDownLatch and CyclicBarrier?
  • What are the scenarios suitable for using Fork/Join framework?
  • What is the difference between RecursiveTask and RecursiveAction class?
  • In Java 8, can we process stream operations with a Thread pool?
  • What are the scenarios to use parallel stream in Java 8?
  • How Stack and Heap work in Java multi-threading environment?
  • How can we take Thread dump in Java?
  • Which parameter can be used to control stack size of a thread in Java?
  • There are two threads T1 and T2? How will you ensure that these threads run in sequence T1, T2 in Java?

Multithreading Interview Questions

Describe synchronization with respect to multithreading?
Ans: With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, one thread can modify a shared variable while another thread is in the process of using or updating the same shared variable. This usually leads to significant errors.

Explain the different ways of using threads?
Ans: The thread could be implemented by using a runnable interface or by inheriting from the Thread class. The former is more advantageous cause when you are going for multiple inheritances, and the only interface can help.

What are the daemon threads, and which method is used to create them?
Ans: The Daemon threads are the low priority threads that run intermittently in the background, performing the garbage collection operation for the java runtime system. “setDaemon” method is used to create a daemon thread.

Can applets communicate with each other?
Ans: Overridden At this point, applets may communicate with other applets running in the same virtual machine. If the applets are of the same class, they can communicate via shared static variables. If the applets are of different classes, then each will need a reference to the same class with static variables. In any case, the basic idea is to pass the information back and forth through a static variable.

An applet can also get references to all other applets on the same page using the getApplets() method of java.applet.AppletContext. Once you get the reference to an applet, you can communicate with it by using its public members. It is conceivable to have applets in different virtual machines that talk to a server somewhere on the Internet and store any data that needs to be serialized there. Then, when another applet needs this data, it could connect to this same server. This implementation is nontrivial.

What method must be implemented by all threads?
Ans: All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.

What are synchronized methods and synchronized statements?
Ans: Overriding Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method’s object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

What is the difference between pre-emptive scheduling and time slicing?
Ans: Under pre-emptive scheduling, the highest priority task executes until it enters the waiting or dead states, or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then re-enters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.

What is synchronization, and why is it important?
Ans: Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time. Without synchronization, one thread can modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.

Can a lock be acquired in a class?
Ans: Yes, a lock can be acquired on a class. This lock is acquired on the class’s Class object.

What’s new with the stop(), suspend() and resume() methods in JDK 1.2?
Ans: The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.

What state does a thread enter when it terminates its processing?
Ans: When a thread terminates its processing, it enters the dead state.

What is the difference between yielding and sleeping?
Ans: When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep()method, it returns to the waiting state.

What is the difference between preemptive scheduling and time slicing?
Ans: Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states, or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other
factors.

What is the catch or declare rule for method declarations?
Ans: If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause.

What is a task’s priority, and how is it used in scheduling?
Ans: A task’s priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.

When is a thread created and started, what is its initial state?
Ans: A thread is in the ready state after it has been created and started.

What invokes a thread’s run() method?
Ans: After a thread is started, via its start() method or that of the Thread class, the JVM invokes the threads run() method when the thread is initially executed.

What is the purpose of the wait(), notify(), and notifyAll() methods?
Ans: The wait(), notify(), and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object’s wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object’s notify() or notifyAll() methods.

What are the high-level thread states?
Ans: The high-level thread states are ready, running, waiting, and dead.

What is an object’s lock, and which objects have locks?
Ans: An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock. All objects and classes have locks. A class’s lock is acquired on the class’s Class object.

What happens when a thread cannot acquire a lock on an object?
Ans: If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object’s lock, it enters the waiting state until the lock becomes available.

How does multithreading take place on a computer with a single CPU?
Ans: The operating system’s task scheduler allocates execution time to multiple tasks. By quickly switching between executing tasks, it creates the impression that tasks execute sequentially.

When is the finally clause of a try-catch-finally statement executed?
Ans: The finally block or clause of the try-catch-finally statement is always executed unless the thread of execution terminates or an exception occurs within the execution of the finally block or clause.

What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?
Ans: When a task’s interrupt() method is executed, the task enters the ready state. The next time the task enters the running state, an Interrupted Exception is thrown.

What are three ways in which a thread can enter the waiting state?
Ans: A thread can enter the waiting state by invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object’s lock, or by invoking an object’s wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.

What happens if a try-catch-finally statement does not have a catch clause to handle an exception that is thrown within the body of the try statement?
Ans: The exception propagates up to the next higher level try-catch statement (if any) or results in the program’s termination.

What method must be implemented by all threads?
Ans: All tasks must implement the run() method, whether they are a subclass of Thread or implement the runnable interface.

What are synchronized methods and synchronized statements?
Ans: Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method’s object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.

What are the two basic ways in which classes that can be run as threads may be defined?
Ans: A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.

What is the difference between process and thread?
Ans: The process is a program in execution, whereas thread is a separate path of execution in a program.

What is multithreading, and what are the methods for inter-thread communication, and what is the class in which these methods are defined?
Ans: Multithreading is the mechanism in which more than one thread runs independently of each other within the process. Wait (), notify (), and notifyAll() methods can be used for interthread communication, and these methods are in Object class.

  • Wait (): When a thread executes a call to wait() method, it surrenders the object lock and enters into a waiting state.
  • Notify() or notifyAll(): To remove a thread from the waiting state, some other thread must make a call to notify() or notifyAll() method on the same object.

What is the class and interface in java to create a thread, and which is the most advantageous method?
Ans: Thread class and Runnable interface can be used to create threads, and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here.

What are the states associated with the thread?
Ans: The thread contains ready, running, waiting, and dead states.

When will you synchronize a piece of your code?
Ans: When you expect different threads will access your code, and these threads may change a particular data causing data corruption.

What is deadlock?
Ans: When two threads are waiting for each other and can’t precede the program is said to be deadlock.

Ref: article

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment