Govt Exams
ThreadLocal values persist across task executions in the same thread. ThreadPools reuse threads, so previous ThreadLocal values remain unless explicitly removed. This can cause data leakage. Developers must call remove() to clean up.
Virtual Threads are lightweight and can be created in large numbers (millions) with minimal memory footprint, unlike platform threads which are heavy OS-level constructs. This solves scalability issues in high-concurrency applications.
StampedLock provides optimistic reads that don't require acquiring a lock. If the data changes during an optimistic read, validation fails and a pessimistic lock can be acquired.
Thread t = new Thread(() -> { throw new RuntimeException("Error"); });
t.setUncaughtExceptionHandler((thread, ex) -> System.out.println("Caught"));
t.start();
The UncaughtExceptionHandler is invoked when an exception is thrown in a thread and not caught. It will print 'Caught' before the thread terminates.
ForkJoinPool uses a work-stealing algorithm where idle threads can 'steal' tasks from busy threads' queues, providing better load balancing for divide-and-conquer problems.
Livelock occurs when threads are not blocked but continuously change state in response to each other (like two people trying to pass each other), preventing progress. This differs from deadlock where threads are blocked.
ReentrantReadWriteLock allows multiple threads to acquire the read lock simultaneously, but only one thread can hold the write lock. This improves concurrency for read-heavy workloads.
AtomicInteger uses lock-free CAS operations which are more efficient than synchronized blocks or locks for simple counter operations with multiple threads.
BlockingQueue (like LinkedBlockingQueue) is purpose-built for producer-consumer patterns, handling synchronization and blocking elegantly.
strictfp ensures consistent floating-point results across different platforms/JVMs, though it's not directly a multithreading construct.