Threads, Runnable, synchronized, volatile, ExecutorService, CompletableFuture, locks, atomics, concurrent collections and virtual threads.
40 cards · mixed cards · AI-written, checked twice. Edit anything.
- Why is implementing Runnable preferred to extending Thread?
Show answer
Java doesn't support multiple inheritance, so extending Thread locks you into that hierarchy. Runnable is an interface, allowing classes to extend another class too.
- What does Thread.start() do?
Show answer
Starts a new thread and calls run() inside that thread. Does not execute run() in the current thread.
- What happens if you call Thread.run() directly?
Show answer
The run() method executes in the current thread, not a new thread. No concurrent execution occurs.
- How does the synchronized keyword protect code?
Show answer
It uses the object's monitor (intrinsic lock) to ensure only one thread can execute the synchronized block or method at a time.
- Can a synchronized method call another synchronized method on the same object without deadlock?
Show answer
Yes, because ReentrantLock behavior is built into Java monitors. The same thread can reacquire the lock.
- What does the volatile keyword guarantee?
Show answer
It ensures that changes to a volatile variable are immediately visible to all threads. It does not provide atomicity.
- How does volatile differ from synchronized in terms of performance?
Show answer
Volatile is lighter-weight than synchronized. It only ensures visibility, not mutual exclusion, so multiple threads can access a volatile variable simultaneously.
- What does ExecutorService abstract?
Show answer
It abstracts thread creation and management, allowing you to submit tasks without manually creating and managing Thread objects.
- What does ExecutorService.newFixedThreadPool(10) create?
Show answer
A thread pool with 10 threads that reuse themselves for submitted tasks. The pool size is fixed.
- How do execute() and submit() differ in ExecutorService?
Show answer
execute(Runnable) returns void and throws checked exceptions. submit() returns a Future and wraps checked exceptions in unchecked exceptions.
- What is the difference between shutdown() and shutdownNow() on ExecutorService?
Show answer
shutdown() stops accepting new tasks but waits for existing tasks to complete. shutdownNow() stops accepting new tasks and attempts to interrupt running tasks.
- What does CompletableFuture.thenApply() do?
Show answer
It takes a Function that transforms the result of the previous stage and returns a new CompletableFuture with the transformed result.
- What does CompletableFuture.thenAccept() take as a parameter?
Show answer
A Consumer that accepts the result but returns void. It does not transform the value.
- How does CompletableFuture.join() differ from get()?
Show answer
join() returns the result or throws an unchecked CompletionException. get() throws checked ExecutionException and InterruptedException.
- What type of lambda does CompletableFuture.supplyAsync() accept?
Show answer
A Supplier with no parameters that returns a value. It is used to asynchronously compute and provide a result.