Practice & Assessment
Test your understanding of Concurrency and Multithreading
Multiple Choice Questions
5What is the result of calling run() instead of start() on a Thread?
Two threads both call increment() on the same counter without synchronization. The counter starts at 0 and each thread increments 1000 times. What is the most likely result?
What happens if you never call shutdown() on an ExecutorService?
What advantage does AtomicInteger have over a synchronized counter?
What is the difference between thenApply and thenCompose in CompletableFuture?
Coding Challenges
1Parallel file word counter
Given a directory path, use an ExecutorService with a fixed thread pool of 4 threads to count total words across all .txt files concurrently. Each task processes one file and returns a Callable<Long> with the word count. Collect all Future<Long> results and sum them. Print per-file counts and total. Handle InterruptedException and ExecutionException properly. Test with at least 8 text files of varying sizes. Input: directory path as a String argument. Output: per-file word count and grand total. Time estimate: 25โ30 minutes.
Mini Project
Multi-threaded Log Processor
Build a log file processor that reads a large log file (generate it synthetically with 100,000 lines), filters lines by log level (INFO, WARN, ERROR), and writes filtered output to separate files concurrently. Use a BlockingQueue as a buffer between a single producer thread (reads lines) and multiple consumer threads (filter and write). Create a LogEntry record with fields: timestamp, level, message. Producer parses each line into LogEntry and puts it on the queue. Three consumer threads each handle one log level, take from the queue, and write to level-specific output files using BufferedWriter (each writer is thread-local to avoid contention). Use a poison-pill pattern (null LogEntry) to signal consumers to stop. Report total lines processed per level on completion.
