Synchronization:
- The concept of allowing thread sequentially when multiple threads trying to access a resource.
- Every application has critical section need to be synchronized.
- We cannot synchronize the entire object.
- We can synchronize only a block or method.
- “synchronized” keyword is used to synchronize the method.
class Resource
{
static int x=0;
static void modifyX()
{
++x;
}
}
class Custom1 extends Thread
{
public void run()
{
for (int i=1 ; i<=100000 ; i++)
{
Resource.modifyX();
}
}
}
class Synchronization
{
public static void main(String[] args) throws Exception
{
Custom1 t1 = new Custom1();
t1.start();
t1.join();
System.out.println("Final x value is : " + Resource.x);
}
}
- We must define the method using synchronized keyword.
- Only one thread can accessed that method.
class Resource
{
static int x=0;
synchronized static void modifyX()
{
++x;
}
}
class Custom1 extends Thread
{
public void run()
{
for (int i=1 ; i<=100000 ; i++)
{
Resource.modifyX();
}
}
}
class Custom2 extends Thread
{
public void run()
{
for (int i=1 ; i<=100000 ; i++)
{
Resource.modifyX();
}
}
}
class Custom3 extends Thread
{
public void run()
{
for (int i=1 ; i<=100000 ; i++)
{
Resource.modifyX();
}
}
}
class Synchronization
{
public static void main(String[] args) throws Exception
{
Custom1 t1 = new Custom1();
Custom2 t2 = new Custom2();
Custom3 t3 = new Custom3();
t1.start();
t2.start();
t3.start();
t1.join();
t2.join();
t3.join();
System.out.println("Final x value is : " + Resource.x);
}
}
ATM application withdraw() method synchronization:
- We cannot perform 2 withdraw transactions using single atm card parallel.
- The following example creating the problem while updating the balance in account when we try to perform 2 money transactions using single atm card.
class InsufficientFundsException extends Exception
{
String name;
InsufficientFundsException(String name)
{
this.name = name;
}
String getErrorMessage(){
return this.name;
}
}
class Account{
private int balance;
Account(int balance)
{
this.balance = balance ;
}
public int getBalance()
{
return this.balance;
}
synchronized void withdraw(int amount) throws InsufficientFundsException
{
System.out.println("Trying to withdraw : "+amount);
System.out.println("Balance in account : "+this.balance);
if(amount <= this.balance)
{
System.out.println("Collect the cash : "+amount);
this.balance = this.balance - amount ;
}
else
{
throw new InsufficientFundsException("No funds.......");
}
}
}
class AccountThread extends Thread
{
Account atm ;
int amt ;
AccountThread(Account atm , int amt)
{
this.atm = atm ;
this.amt = amt ;
}
public void run()
{
try
{
atm.withdraw(amt);
}
catch (InsufficientFundsException ie)
{
System.out.println("Error : "+ie.getErrorMessage());
}
}
}
class ATMApplication
{
public static void main(String[] args) throws InterruptedException
{
Account atm = new Account(6000);
System.out.println("Initial amount in the account : "+atm.getBalance());
AccountThread t1 = new AccountThread(atm, 4000);
AccountThread t2 = new AccountThread(atm, 3000);
t1.start();
t2.start();
t1.join();
t2.join();
System.out.println("Final amount in the account : "+atm.getBalance());
}
}