I tried to create a race condition like this.
class Bankaccount { private int balance=101; public int getBalance(){ return balance; } public void withdraw(int i){ balance=balance-i; System.out.println("..."+balance); } } public class Job implements Runnable{ Bankaccount b=new Bankaccount(); public void run(){ if(b.getBalance()>100){ System.out.println("the balanced ammount is"+b.getBalance()); /*try{ Thread.sleep(9000); } catch(Exception e){ }*/ makeWithdrawl(100); } } public void makeWithdrawl(int ammount){ b.withdraw(ammount); System.out.println(b.getBalance()); } public static void main(String[] args) { Job x=new Job(); Job y=new Job(); Thread t1=new Thread(x); Thread t2=new Thread(y); t1.start(); t2.start(); } } I am getting output: the balanced ammount is101 ...1 1 the balanced ammount is101 ...1
I was expecting it to be in negative as two times withdrawal happened for 100
What is missing here? Thanks in Advance
BankAccountso I cannot see any way to create a race condition here.