[Go to site: main page, start]

0% found this document useful (0 votes)
3 views3 pages

Java Interview Multithreading

The document contains Java interview questions focused on multithreading and concurrency. Key topics include the definition of a thread, differences between extending Thread and implementing Runnable, thread lifecycle, synchronization, deadlocks, ExecutorService, and the use of volatile and ThreadLocal keywords. It provides essential concepts and distinctions relevant to Java multithreading for interview preparation.

Uploaded by

gefehif698
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views3 pages

Java Interview Multithreading

The document contains Java interview questions focused on multithreading and concurrency. Key topics include the definition of a thread, differences between extending Thread and implementing Runnable, thread lifecycle, synchronization, deadlocks, ExecutorService, and the use of volatile and ThreadLocal keywords. It provides essential concepts and distinctions relevant to Java multithreading for interview preparation.

Uploaded by

gefehif698
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Interview Questions

Multithreading & Concurrency

Q1. What is a Thread in Java?


A thread is the smallest unit of execution within a process. Java provides multithreading,
allowing concurrent execution of multiple parts of a program for maximum utilization of
the CPU. A thread can be created by extending the Thread class or implementing the
Runnable interface.

Q2. Difference between extending Thread and implementing


Runnable?
Implementing Runnable is preferred because Java doesn't support multiple inheritance;
if a class extends Thread, it cannot extend any other class. Runnable also enforces
separation of the task from the runner, making it more object-oriented.

Q3. Explain the Thread Lifecycle.


A thread transitions through several states: NEW (created but not started), RUNNABLE
(executing in the JVM), BLOCKED (waiting for a monitor lock), WAITING (waiting
indefinitely for another thread), TIMED_WAITING (waiting for a specified time), and
TERMINATED (completed execution).

Q4. What does the 'synchronized' keyword do?


It ensures that only one thread can execute a block of code or method at a given time
for a specific object lock. It prevents race conditions and ensures memory visibility
across threads, protecting shared resources.
Q5. What is a Deadlock and how can you prevent it?
A deadlock occurs when two or more threads are blocked forever, waiting for each other
to release locks. It can be prevented by avoiding nested locks, ensuring all threads
acquire locks in the same specific order, and using lock timeouts.

Q6. What is ExecutorService?


ExecutorService is a higher-level API for managing thread pools. It separates thread
creation from execution, managing a pool of worker threads. You can submit tasks
(Runnable/Callable) to it, and it returns Futures to track execution results.

Q7. Difference between wait() and sleep()?


wait() is an Object method that releases the lock and is used for inter-thread
communication. sleep() is a static Thread method that pauses the thread for a specified
duration but does not release any locks it holds.

Q8. What are Daemon threads?


Daemon threads are low-priority background threads that provide services to user
threads (e.g., Garbage Collection). The JVM terminates itself automatically when all
user threads finish execution, regardless of running daemon threads.

Q9. What is the 'volatile' keyword?


The volatile keyword tells the JVM that a variable's value may be changed by multiple
threads. It ensures that reads and writes bypass thread-local caches and go directly to
main memory, providing visibility of the latest value to all threads.
Q10. What is ThreadLocal?
ThreadLocal provides thread-local variables. Each thread accessing the variable has its
own independently initialized copy, avoiding the need for synchronization when dealing
with non-thread-safe objects like SimpleDateFormat.

Generated Document • Multithreading & Concurrency

You might also like