Threading & Synchronization
Race Condition
Two threads accessing shared data concurrently causing incorrect results.
synchronized vs ReentrantLock
| Feature | synchronized | ReentrantLock |
|---|---|---|
| Timeout | No | tryLock(timeout) |
| Fairness | No | Can be fair |
| Interruptible | No | lockInterruptibly() |
Deadlock
Circular wait: Thread 1 holds A waits B, Thread 2 holds B waits A.
Prevention: Lock ordering, tryLock timeout, avoid nested locks.
Key Points
- Understanding Concurrency Questions is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Java Concurrency Utilities
ExecutorService
ExecutorService executor = Executors.newFixedThreadPool(10);
scheduler.scheduleAtFixedRate(task, 0, 1, TimeUnit.MINUTES);
Atomic Variables
AtomicInteger counter = new AtomicInteger(0);
counter.incrementAndGet();
counter.compareAndSet(5, 10);
CompletableFuture
CompletableFuture.supplyAsync(() -> fetchData())
.thenApply(data -> transform(data))
.thenAccept(result -> save(result));
Detect Deadlocks
jstack, jconsole, ThreadMXBean.findDeadlockedThreads()
Practice Problems
Design and implement a solution for Concurrency Questions in a backend system. Consider scalability, error handling, and production readiness.
Solution
// Concurrency Questions implementation
// Key aspects: validation, error handling, logging, testing
public class ConcurrencyQuestions {
// Production-ready implementation
}Identify and handle edge cases for Concurrency Questions. What happens under high load, with invalid input, or during failures?
Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotencyWrite a testing strategy for Concurrency Questions. Include unit tests, integration tests, and performance tests.
Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injectionQuiz
1. How to prevent deadlocks?
2. synchronized vs volatile difference?
3. What is the primary purpose of Concurrency Questions?
4. What is a common mistake when implementing Concurrency Questions?
Flashcards
Question
What causes deadlocks?
Click to reveal answer
Answer
Circular wait - two threads holding and waiting for each others locks
Question
synchronized vs volatile?
Click to reveal answer
Answer
synchronized: exclusion + visibility. Volatile: visibility only
Question
What is Concurrency Questions?
Click to reveal answer
Answer
Concurrency Questions is a key concept in backend development.
Question
When to use Concurrency Questions?
Click to reveal answer
Answer
Use Concurrency Questions when building production systems that require reliability, scalability, and maintainability.
Question
Concurrency Questions best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1.Race condition: shared data + wrong timing
- 2.Deadlock: circular wait - prevent with lock ordering
- 3.synchronized = exclusion + visibility, volatile = visibility only
- 4.Use ExecutorService and CompletableFuture
Interview Tips
- •Draw diagrams of race conditions and deadlocks
- •Know how to detect deadlocks in production
Cheat Sheet
Concurrency Interview
- Race Condition: Two threads, shared data, wrong timing
- Deadlock: Circular lock wait
- Prevention: Lock ordering, tryLock(timeout)
- synchronized: Exclusion + Visibility
- volatile: Visibility only