1/56
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is concurrency in Java?
Concurrency is the ability of a program to execute multiple tasks simultaneously or out of order to maximize efficiency.
What is multithreading in Java?
A programming technique where multiple threads run in parallel within a single process.
What is a thread?
A lightweight subprocess that can execute independently within a program.
Which class represents a thread in Java?
java.lang.Thread
How can you create a thread in Java?
By extending the Thread
class or implementing the Runnable
interface.
What is the difference between Runnable and Thread?
Runnable
is a functional interface; Thread
is a class that can be subclassed.
What method starts a thread?
start()
What method contains the thread's task?
run()
What is the difference between start()
and run()
?
start()
creates a new thread; run()
executes in the current thread.
What is the lifecycle of a thread?
New → Runnable → Running → Blocked/Waiting → Terminated
What is thread synchronization?
A technique to control access to shared resources to avoid race conditions.
Which keyword is used for synchronization in Java?
synchronized
What is a race condition?
A situation where multiple threads access shared data and produce unpredictable results.
What is a critical section?
A part of code that must be executed by only one thread at a time.
How do you synchronize a method?
By adding the synchronized
keyword to its declaration.
How do you synchronize a block of code?
Using synchronized(obj)
to lock a specific object.
What is intrinsic lock or monitor lock?
A lock associated with every object that is used to implement synchronization.
What is deadlock in Java?
A situation where two or more threads are waiting for each other to release resources, causing all to block indefinitely.
What is livelock?
A situation where threads keep changing state in response to each other without making progress.
What is thread starvation?
When a thread is unable to gain regular access to resources due to high-priority threads.
What is wait() in Java?
A method used to make a thread wait until it is notified.
What is notify() in Java?
A method used to wake up a single thread waiting on the object's monitor.
What is notifyAll()?
It wakes up all threads waiting on the object's monitor.
What is the difference between wait() and sleep()?
wait()
releases the monitor lock; sleep()
does not.
Which class provides high-level concurrency utilities?
java.util.concurrent
What is an ExecutorService?
A high-level replacement for managing threads manually.
Which method is used to submit tasks to ExecutorService?
submit()
or execute()
What is Future in Java?
A result placeholder for asynchronous computation.
How do you retrieve a result from Future?
Using future.get()
What is Callable in Java?
A task that returns a result and may throw an exception.
How is Callable different from Runnable?
Callable returns a value; Runnable does not.
What is ThreadPoolExecutor?
A class for managing a pool of reusable threads.
What is ScheduledExecutorService?
An executor that can run tasks periodically or after a delay.
What are Atomic classes?
Classes like AtomicInteger
, AtomicBoolean
that provide lock-free thread-safe operations.
What is ReentrantLock?
An advanced locking mechanism that offers more flexibility than synchronized
.
What is the advantage of ReentrantLock?
Ability to try lock, interrupt lock acquisition, and fair ordering.
What is ReadWriteLock?
A lock that allows multiple readers or one writer at a time.
What is a Semaphore?
A concurrency utility that controls access to a resource with a set number of permits.
What is a CountDownLatch?
A synchronization aid that allows threads to wait until a set of operations completes.
What is a CyclicBarrier?
A barrier that lets threads wait for each other before continuing.
What is a Phaser in Java?
A flexible synchronization barrier that supports dynamic registration of parties.
What is ForkJoinPool?
A framework for parallelism using divide-and-conquer tasks, introduced in Java 7.
What is the common pool in ForkJoinPool?
A shared pool used by the parallelStream()
and CompletableFuture
.
What is the difference between parallelism and concurrency?
Concurrency is about structure and interleaving; parallelism is about executing multiple threads simultaneously.
What is volatile keyword in Java?
Ensures visibility of changes to variables across threads.
How does volatile differ from synchronized?
volatile
ensures visibility, not atomicity; synchronized
provides both.
What is ThreadLocal in Java?
A variable that is accessible only by the thread that created it.
What is a daemon thread?
A background thread that terminates when all user threads are finished.
How do you create a daemon thread?
By calling setDaemon(true)
before start()
.
What is the default priority of a thread?
Thread.NORM_PRIORITY
(value 5)
Can thread priorities be changed?
Yes, using setPriority()
, but behavior is OS-dependent.
What is a ConcurrentHashMap?
A thread-safe implementation of a hash map for high-concurrency scenarios.
What is the role of BlockingQueue?
A thread-safe queue that waits for availability when inserting or removing elements.
Give an example of BlockingQueue implementation.
ArrayBlockingQueue
, LinkedBlockingQueue
What are parallel streams in Java 8?
Streams that divide elements and process them in multiple threads using the ForkJoinPool.
Should shared mutable state be used in parallel streams?
No, it can lead to race conditions.
What are some best practices for concurrency in Java?
Minimize shared state, prefer higher-level concurrency APIs, use immutability when possible.