- What is concurrency in Java?
- Concurrency in Java is the ability to run several programs or several parts of a program in parallel to maximize the utilization of CPU resources.
- How do you create a thread in Java?
- You can create a thread in Java by either extending the
Thread
class or implementing theRunnable
interface.
- You can create a thread in Java by either extending the
- What is the difference between the
Runnable
interface and theThread
class?Runnable
is an interface that allows a class to be executed by a thread.Thread
is a class that represents a thread of execution. ImplementingRunnable
is preferred because it supports composition and avoids the Java single inheritance issue.
- How do you start a thread in Java?
- To start a thread, you instantiate a
Thread
object and call itsstart()
method.
- To start a thread, you instantiate a
- What does the
synchronized
keyword mean?- The
synchronized
keyword is used to ensure that a method or block is accessed by only one thread at a time.
- The
- What is the difference between
wait()
andsleep()
in Java?wait()
releases the lock or monitor, whilesleep()
does not.wait()
is called on an object, butsleep()
is called on aThread
.
- What is a deadlock?
- A deadlock is a situation where two or more threads are blocked forever, waiting for each other.
- How can you prevent a deadlock in Java?
- You can prevent deadlocks by avoiding nested locks, using a lock ordering convention, or using a timeout when trying to obtain locks.
- What is the
volatile
keyword in Java?- The
volatile
keyword indicates that a variable’s value will be modified by different threads and ensures the visibility of changes to variables across threads.
- The
- What are atomic operations in Java?
- Atomic operations are operations that are performed in a single unit of task without interference from other operations. Java provides atomic classes in the
java.util.concurrent.atomic
package, such asAtomicInteger
, for this purpose.
- Atomic operations are operations that are performed in a single unit of task without interference from other operations. Java provides atomic classes in the
- What is the Executor Framework in Java?
- The Executor Framework is a framework provided by the
java.util.concurrent
package that simplifies the execution of tasks in asynchronous mode.
- The Executor Framework is a framework provided by the
- How does the
synchronized
keyword differ from locks in Java?- While
synchronized
is a keyword that provides a built-in locking mechanism, locks (from thejava.util.concurrent.locks
package) offer more sophisticated thread synchronization control through lock and unlock methods.
- While
- What is a
Semaphore
in Java?- A
Semaphore
is a concurrency utility that controls access to a shared resource through the use of counters.
- A
- What is a
CountDownLatch
in Java?- A
CountDownLatch
is used to make one thread wait until one or more threads have completed their operations.
- A
- What is a
CyclicBarrier
in Java?- A
CyclicBarrier
is a synchronization mechanism that allows a set of threads to all wait for each other to reach a common barrier point.
- A
- Explain the producer-consumer problem in Java.
- The producer-consumer problem is a classic example of a multi-process synchronization problem, where the goal is to ensure that the producer doesn’t produce items when the buffer is full, and the consumer doesn’t consume items when the buffer is empty.
- What is
ThreadLocal
in Java?ThreadLocal
is a class that provides thread-local variables. Each thread accessing such a variable has its own, independently initialized copy of the variable.
- How do you use the
ReentrantLock
class in Java?ReentrantLock
is used for locking with the same basic behavior and semantics as the implicit monitor lock accessed usingsynchronized
methods and statements, but with extended capabilities.
- What are the key components of the Java Concurrency API?
- Key components include
ExecutorService
,Locks
,Atomic Variables
,Futures
,Blocking Queues
, and theFork/Join Framework
.
- Key components include
- What is the
Fork/Join Framework
in Java?- It is a framework designed for work-stealing algorithms, facilitating parallel programming by recursively breaking tasks into smaller tasks and using a pool of threads to execute these tasks.
- How can you stop a thread in Java?
- Stopping a thread safely is done by using interrupt signals (
interrupt()
method) and checking the interrupt status of the thread.
- Stopping a thread safely is done by using interrupt signals (
- What is
Thread.join()
in Java?- The
join()
method allows one thread to wait for the completion of another.
- The
- What is the significance of the
ConcurrentHashMap
in Java?ConcurrentHashMap
is a thread-safe variant ofHashMap
that does not lock the Map for reads and reduces lock contention for writes.
- How does
synchronized
block differ fromsynchronized
method?- A
synchronized
block provides synchronization only for the block of code it encloses, offering more granular control, while asynchronized
method locks the entire object or class.
- A
- What is a
ReadWriteLock
in Java?- A
ReadWriteLock
allows a resource to be accessed by multiple readers or a single writer at a time, but not both.
- A
- Describe the
wait
,notify
, andnotifyAll
methods in Java.- These methods are used for communication between threads.
wait
causes the current thread to wait until another thread invokesnotify
ornotifyAll
.notify
wakes up a single waiting thread, whilenotifyAll
wakes up all waiting threads.
- These methods are used for communication between threads.
- What are the advantages of using the Executor Framework?
- It simplifies the execution of tasks in asynchronous mode, helps manage a pool of threads, and reduces the need for manual thread management.
- How does the
synchronized
keyword ensure visibility and atomicity?- It ensures that changes made by one thread are visible to others, and it provides atomicity by locking the critical section so that only one thread can execute it at a time.
- What is the purpose of
BlockingQueue
in Java?BlockingQueue
is a type of queue that supports operations that wait for the queue to become non-empty when retrieving