public class BankAccountSynchronization {
public class BankAccount {
private double balance;
// Synchronized method to prevent race conditions
public synchronized void deposit(double amount) {
if (amount > 0) {
balance += amount;
System.out.println(
Thread.currentThread().getName() +
" deposited $" + amount
);
}
}
// Synchronized method for thread-safe withdrawal
public synchronized boolean withdraw(double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
System.out.println(
Thread.currentThread().getName() +
" withdrew $" + amount
);
return true;
}
return false;
}
// Atomic get balance operation
public synchronized double getBalance() {
return balance;
}
}
// Simulate concurrent transactions
public void runBankingSimulation() {
BankAccount account = new BankAccount();
// Multiple threads performing transactions
Runnable depositTask = () -> {
for (int i = 0; i < 5; i++) {
account.deposit(100);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
Runnable withdrawTask = () -> {
for (int i = 0; i < 3; i++) {
account.withdraw(50);
try {
Thread.sleep(75);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// Create and start threads
new Thread(depositTask, "Deposit-Thread").start();
new Thread(withdrawTask, "Withdraw-Thread").start();
}
}
public class InventoryManagementSystem {
public class InventoryManager {
private Map inventory = new HashMap<>();
// Synchronized method for adding stock
public synchronized void addStock(String productId, int quantity) {
inventory.merge(productId, quantity, Integer::sum);
System.out.println("Added " + quantity + " of " + productId);
}
// Synchronized method for removing stock
public synchronized boolean removeStock(String productId, int quantity) {
Integer currentStock = inventory.get(productId);
if (currentStock == null || currentStock < quantity) {
System.out.println("Insufficient stock for " + productId);
return false;
}
inventory.put(productId, currentStock - quantity);
System.out.println("Removed " + quantity + " of " + productId);
return true;
}
// Atomic stock retrieval
public synchronized int getStock(String productId) {
return inventory.getOrDefault(productId, 0);
}
}
// Simulate concurrent inventory operations
public void runInventorySimulation() {
InventoryManager inventory = new InventoryManager();
// Initial stock
inventory.addStock("LAPTOP-001", 50);
// Simulate sales
Runnable salesTask = () -> {
for (int i = 0; i < 10; i++) {
inventory.removeStock("LAPTOP-001", 2);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// Simulate restocking
Runnable restockTask = () -> {
for (int i = 0; i < 5; i++) {
inventory.addStock("LAPTOP-001", 5);
try {
Thread.sleep(150);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// Create and start threads
new Thread(salesTask, "Sales-Thread").start();
new Thread(restockTask, "Restock-Thread").start();
}
}
public class ConcurrentCacheSystem {
public class ThreadSafeCache {
private Map cache = new HashMap<>();
private final Object lock = new Object();
// Synchronized block for cache write
public void put(String key, String value) {
synchronized(lock) {
cache.put(key, value);
System.out.println("Cached: " + key + " = " + value);
}
}
// Synchronized method for cache read
public synchronized String get(String key) {
return cache.get(key);
}
// Atomic cache update operation
public void updateCache(String key, String value) {
synchronized(lock) {
String existingValue = cache.get(key);
if (existingValue != null) {
cache.put(key, value);
System.out.println("Updated: " + key);
}
}
}
}
// Simulate concurrent cache operations
public void runCacheSimulation() {
ThreadSafeCache cache = new ThreadSafeCache();
// Cache write threads
Runnable writeTask = () -> {
for (int i = 0; i < 5; i++) {
cache.put("key-" + i, "value-" + i);
try {
Thread.sleep(50);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// Cache read threads
Runnable readTask = () -> {
for (int i = 0; i < 5; i++) {
String key = "key-" + i;
String value = cache.get(key);
System.out.println("Read: " + key + " = " + value);
try {
Thread.sleep(75);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
};
// Create and start threads
new Thread(writeTask, "Write-Thread").start();
new Thread(readTask, "Read-Thread").start();
}
}