Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created August 20, 2025 12:50
Show Gist options
  • Save Audhil/91be1b480ca3d53bdbfb56d04b2cae37 to your computer and use it in GitHub Desktop.
Save Audhil/91be1b480ca3d53bdbfb56d04b2cae37 to your computer and use it in GitHub Desktop.
All about threads!
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
public class Main {
public static void main(String[] args) {
// BankAccount account = new BankAccount();
// Thread t1 = new Thread(() -> account.withdraw(80), "Person-1");
// Thread t2 = new Thread(() -> account.withdraw(80), "Person-2");
// BankAccount2 account = new BankAccount2();
// Thread t1 = new Thread(() -> account.withdraw(80), "Person-1");
// Thread t2 = new Thread(() -> account.withdraw(80), "Person-2");
// BankAccount3 account = new BankAccount3();
// Thread t1 = new Thread(() -> account.withdraw(80), "Person-1");
// Thread t2 = new Thread(() -> account.withdraw(80), "Person-2");
//
// t1.start();
// t2.start();
BankAccount account = new BankAccount();
ExecutorService executor = Executors.newFixedThreadPool(2);
executor.submit(() -> account.withdraw(80));
executor.submit(() -> account.withdraw(80));
executor.shutdown();
}
// 1. with synchronized
private static class BankAccount {
private int balance = 100; // starting balance
public synchronized void withdraw(int amount) {
if (balance >= amount) {
System.out.println(Thread.currentThread().getName() + " is going to withdraw " + amount);
try { Thread.sleep(100); } catch (InterruptedException e) { }
balance -= amount;
System.out.println(Thread.currentThread().getName() + " completed withdrawal. Balance left: " + balance);
} else {
System.out.println("Not enough money for " + Thread.currentThread().getName());
}
}
}
// 2. with Reentrant lock
private static class BankAccount2 {
private int balance = 100;
private final ReentrantLock lock = new ReentrantLock();
public void withdraw(int amount) {
lock.lock(); // acquire lock
try {
if (balance >= amount) {
System.out.println(Thread.currentThread().getName() + " is going to withdraw " + amount);
try { Thread.sleep(100); } catch (InterruptedException e) { }
balance -= amount;
System.out.println(Thread.currentThread().getName() + " completed withdrawal. Balance left: " + balance);
} else {
System.out.println("Not enough money for " + Thread.currentThread().getName());
}
} finally {
lock.unlock(); // release lock
}
}
}
// 3. with atomic integer
private static class BankAccount3 {
private final AtomicInteger balance = new AtomicInteger(100);
public void withdraw(int amount) {
int currentBalance;
do {
currentBalance = balance.get();
if (currentBalance < amount) {
System.out.println("Not enough money for " + Thread.currentThread().getName());
return;
}
} while (!balance.compareAndSet(currentBalance, currentBalance - amount));
System.out.println(Thread.currentThread().getName() + " withdrew " + amount + ". Balance left: " + balance.get());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment