Fundamentals 7 min read

Basic Java Multithreading Interview Questions and Answers

This article presents essential Java multithreading interview questions covering thread creation methods, thread lifecycle states, synchronization differences, monitor mechanisms, deadlock definition, and strategies to avoid deadlocks, providing concise explanations for each concept.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Basic Java Multithreading Interview Questions and Answers

Below are several introductory Java multithreading interview questions that every developer should master.

How many ways can you create a thread? Which one do you prefer and why?

There are three ways to create a thread:

Extending the Thread class

Implementing the Runnable interface

Using the Executor framework to create a thread pool

Implementing Runnable is more popular because it does not require extending Thread , avoiding the need for multiple inheritance (which Java does not support). Additionally, thread pools are efficient and easy to use.

Briefly explain the possible states of a thread.

New (new): A thread object has just been created.

Runnable (runnable): After creation, another thread (e.g., the main thread) calls start() ; the thread is placed in the runnable pool, waiting to be scheduled for CPU time.

Running (running): A runnable thread obtains a CPU time slice and executes its code.

Blocked (blocked): The thread temporarily relinquishes the CPU, for example while waiting, synchronizing, or sleeping, and will return to the runnable state when the condition is cleared.

Blocking can occur in three ways:

Waiting block: a thread calls wait() and is placed in the waiting queue.

Synchronization block: a thread attempts to acquire a lock that is held by another thread and is placed in the lock pool.

Other block: a thread calls sleep() , join() , or performs I/O, causing it to block until the operation completes or the timeout expires.

Dead (dead): The thread’s run() or main() method finishes, or an exception terminates the method, ending the thread’s lifecycle.

What is the difference between synchronized methods and synchronized blocks?

Differences:

Synchronized methods use this or the class object as the lock by default.

Synchronized blocks allow you to choose any object as the lock, providing finer granularity by protecting only the critical section rather than the entire method.

How does a monitor achieve thread synchronization, and what level of synchronization should a program use?

A monitor and a lock are used together in the JVM. A monitor watches a synchronized block, ensuring that only one thread executes the block at a time. Each monitor is associated with an object reference, and a thread must acquire the lock before entering the synchronized code.

Java provides explicit monitors ( Lock ) and implicit monitors ( synchronized ) as two locking mechanisms.

What is a deadlock?

A deadlock occurs when two or more threads are each waiting for the other to release resources, causing all of them to wait indefinitely.

How can you ensure that N threads can access N resources simultaneously without causing a deadlock?

Deadlock arises when four conditions hold simultaneously:

Mutual exclusion: A resource can be used by only one thread at a time.

Hold and wait: A thread holding resources can request additional ones.

No preemption: Resources cannot be forcibly taken from a thread.

Circular wait: A closed chain of threads each waiting for a resource held by the next thread.

Breaking any one of these conditions prevents deadlock. A simple method is to impose a global ordering on lock acquisition and require all threads to acquire locks in that order.

JavadeadlockSynchronizationMultithreadinginterviewThread States
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.