|
for(int i=0;i<n;i++){
O(n)
@Transactional(propagation = Propagation.REQUIRED) public void transferFunds(Account from, Account to, BigDecimal amount) { debit(from, amount); // Deduct from source credit(to, amount); // Add to destination // If credit fails, debit automatically rolls back }
synchronized(lock1)
{
T1 acquires lock1 → Deadlock.
Look for: Found one
Java-level deadlock: Use: jstack PID
|
||||||||||||
|
Use consistent lock ordering. Example: Account first
= from.getId() < to.getId()
SELECT ... FOR UPDATE
No. Volatile provides: Visibility NOT atomicity.
newFixedThreadPool(5) Answers 1. Danger? Yes ExecutorService pool = Executors.newFixedThreadPool(5); // If you submit tasks faster than 5 threads can process, // the internal queue grows indefinitely. for (int i = 0; i < 1_000_000; i++) { pool.submit(() -> heavyTask()); // Queue keeps growing }
|
|
Danger |
Why |
|
Unbounded queue |
Can cause OutOfMemoryError |
|
Deadlock |
Tasks waiting for each other |
|
No backpressure |
System overload without rejection |
|
Silent failures |
Exceptions lost if get() not called |
|
Resource underutilization or overutilization |
Wrong thread count for workload |
LinkedBlockingQueue<String> queue = new LinkedBlockingQueue<>(3);
// Adding elements
queue.put("A"); // Blocks if full
queue.offer("B"); // Returns false if full
queue.add("C"); // Throws exception if full