What is Multithreading? and its advantage?
What is a Thread?
Thread is a small unit of work that can be executed independently and asynchronously.
It's way to take advantage of multiple CPU available in a machine.
Difference between Thread and Process in Java?
A process is a self contained execution environment and it can be seen as a program or application
whereas Thread is a single task of execution within the process.
The Thread is a subset of Process.
How do you implement Thread in Java?
1. Runnable interface
2. Thread class
both has run() method to implement.
When to use Runnable vs Thread in Java?
Difference between start() and run() method of Thread?
Thread Life-cycle?
Difference between Runnable and Callable?
Both Runnable and Callable represent task which is intended to be executed in a separate thread.
Runnable is there from JDK 1.0 while Callable was added on JDK 1.5.
Main difference
Callable - call() method return Future object and throw Exception,
which was not possible with Runnable - run().
What is thread-safety?
What is race condition in Java?
What is volatile variable in Java?
volatile is a special modifier, which can only be used with instance variables.
In concurrent Java programs, changes made by multiple threads on instance variables is not visible to
other in absence of any synchronizers e.g. synchronized keyword or locks.
Volatile variable guarantees that a write will happen before any subsequent read: as stated: "volatile
variable rule" in previous question.
What is Thread Synchronization?
How to accomplish Thread Synchronization?
Synchronized method vs Synchronization block?
Synchronization block can lock on another object.
What is static synchronized method?
Class level lock.
How does thread communicate with each other?
Inter-thread communication using wait(), notify() & notifyAll().
Why wait, notify and notifyAll are not inside thread class?
It is in Object class.
Difference between notify() and notifyAll()?
notify() - resumes "any one" of the Thread that wait on a resource.
notifyAll() - resumes all the Threads that wait on a resource.
Why wait and notify method are called from synchronized block?
Its must to call wait(), notify() & notifyAll() from synchronized block - If not we get
"IllegalMonitorStateException".
A more subtle reason is to avoid the race condition between wait and notify calls.
Can we call run() method of a Thread class?
Yes, we can call run() method of a Thread class but then it will behave like a normal method. To execute
it in a Thread, we need to start it using [Link]() method.
How do you share data between two thread in Java?
You can share data between threads by using shared-resource.
Difference between CyclicBarrier and CountDownLatch in Java??
What is ThreadLocal?
ThreadLocal variables are special kind of variable available to Java programmer that get created for each
Thread. (Just like instance variable is per instance)
It's a nice way to achieve thread-safety of expensive-to-create objects, for example you can make
SimpleDateFormat thread-safe using ThreadLocal. Since that class is expensive, its not good to use it in
local scope, which requires separate instance on each invocation. By providing each thread their own
copy, you shoot two birds with one arrow.
Difference between synchronized and concurrent collection in Java?
Producer Consumer problem?
There are three threads T1, T2, and T3? How do you ensure sequence T1, T2, T3 in Java?
Sequencing in multi-threading can be achieved by using join().
yield()?
To switch from "Running" to "Runnable" - a Thread willing give up the process.
wait() vs sleep()?
What is Deadlock? Write a sample program?
How to prevent Deadlock?
Thread Priority?
Daemon Thread?
How to create daemon thread in Java?
Thread class setDaemon(true) can be used to create daemon thread in java. We need to call this
method before calling start() method else it will throw IllegalThreadStateException.
What is atomic operation? What are atomic classes in Java Concurrency API?
Atomic operations are performed in a single unit of task without interference from other operations.
Atomic operations are necessity in multi-threaded environment to avoid data inconsistency.
int++ is not an atomic operation. So by the time one threads read it’s value and increment it by one,
other thread has read the older value leading to wrong result.
To solve this issue, we will have to make sure that increment operation on count is atomic, we can do
that using Synchronization but Java 5 [Link] provides wrapper classes for int and
long that can be used to achieve this atomically without usage of Synchronization. Go to this article to
learn more about atomic concurrent classes.
What is AutomicVariable?
How do you check if a Thread holds a lock or not?
holdsLock()
Lock interface?
The major advantage of lock interfaces on multi-threaded and concurrent programming is they provide
two separate lock for reading and writing which enables you to write high performance data structure
like ConcurrentHashMap and conditional blocking. This java threads interview question is getting
increasingly popular and more and more follow-up questions come based upon answer of interviewee. I
would strongly suggest reading Locks before appearing for any java multi-threading interview because
now days Its heavily used to build cache for electronic trading system on client and exchange
connectivity space.
What is Executors Framework?
ExecutorService executorService = [Link]();
/* -or- */
//ExecutorService executorService = [Link](2);
// Shutdown Tasks to ExecutorService
[Link](new Task("FIRST"));
[Link](new Task("SECOND"));
// Shutdown ExecutorService - Shuts-down after all the submitted Task are finished
[Link]();
// wait till all Task are finished.
while([Link]());
Executor's execute() vs ExecutorService's submit()
- only Runnable as argument - Runnable and Callable as argument
- void return type - Future as return type
What is Future?
What is FutureTask?
It is a concrete class implementation of Future. It represents a Cancellable Asynchronous Task.
It can wrap both Runnable and Callable objects and it can be submitted to an ExecutorService.