import java.io.*;
import java.util.*;
class Account
{
String name;
int amount,lock;
Account(String name,int amount,int lock)
{
this.name=name;
this.amount=amount;
this.lock=lock;
}
public void deposit(int amt)
{
amount +=amt;
}
public void withdraw(int amt)
{
if((amount-amt)<0)
{
System.out.println("Transaction Cannot proceed : Not Enough Balance in Account"+name);
System.exit(0);
}
amount -=amt;
}
public int balance()
{
return amount;
}
}
class Transaction implements Runnable
{
private Thread t=null;
String id;
Account from,to;
int amount;
Transaction(String id,Account from,Account to,int amount)
{
this.id=id;
this.from=from;
this.to=to;
this.amount=amount;
}
public void acquireLock(Account x,int type)
{
x.lock=type;
}
public void releaseLock(Account x)
{
x.lock=0;
}
public void run() {
System.out.println("Running Transaction" + id );
try {
acquireLock(from,2);
acquireLock(to,2);
from.withdraw(amount);
to.deposit(amount);
System.out.println("Transaction "+id+" is completed");
releaseLock(from);
releaseLock(to);
System.out.println("Balance of Account "+from.name+" is "+from.balance());
System.out.println("Balance of Account "+to.name+" is "+to.balance());
// Let the thread sleep for a while.
Thread.sleep(50);
}
catch (InterruptedException e) {
System.out.println("Transaction " + id + " interrupted.");
}
System.out.println("Transaction " + id + " exiting.");
}
public void start ()
{
System.out.println("Starting Transaction " + id );
while(!(from.lock==0 && to.lock==0));
if(t==null)
{
t = new Thread(this,this.id);
t.start ();
}
}
}
public class twophase
{
public static void main(String args[])
{
Account A=new Account("A",500,0);
Account B=new Account("B",800,0);
Account C=new Account("C",900,0);
Transaction T1 = new Transaction( "T-1",A,B,200);
T1.start();
Transaction T2 = new Transaction( "T-2",C,B,500);
T2.start();
Transaction T3 = new Transaction( "T-3",A,B,400);
T3.start();
}
}
*******************OUTPUT*********************
aiktc@aiktc17 ~ $ javac twophase.java
aiktc@aiktc17 ~ $ java twophase
Starting Transaction T-1
Starting Transaction T-2
Running TransactionT-1
Starting Transaction T-3
Transaction T-1 is completed
Running TransactionT-2
Balance of Account A is 300
Transaction T-2 is completed
Balance of Account C is 400
Balance of Account B is 1500
Balance of Account B is 1500
Running TransactionT-3
Transaction Cannot proceed : Not Enough Balance in AccountA
import java.util.*;
class Account
{
String name;
int amount,lock;
Account(String name,int amount,int lock)
{
this.name=name;
this.amount=amount;
this.lock=lock;
}
public void deposit(int amt)
{
amount +=amt;
}
public void withdraw(int amt)
{
if((amount-amt)<0)
{
System.out.println("Transaction Cannot proceed : Not Enough Balance in Account"+name);
System.exit(0);
}
amount -=amt;
}
public int balance()
{
return amount;
}
}
class Transaction implements Runnable
{
private Thread t=null;
String id;
Account from,to;
int amount;
Transaction(String id,Account from,Account to,int amount)
{
this.id=id;
this.from=from;
this.to=to;
this.amount=amount;
}
public void acquireLock(Account x,int type)
{
x.lock=type;
}
public void releaseLock(Account x)
{
x.lock=0;
}
public void run() {
System.out.println("Running Transaction" + id );
try {
acquireLock(from,2);
acquireLock(to,2);
from.withdraw(amount);
to.deposit(amount);
System.out.println("Transaction "+id+" is completed");
releaseLock(from);
releaseLock(to);
System.out.println("Balance of Account "+from.name+" is "+from.balance());
System.out.println("Balance of Account "+to.name+" is "+to.balance());
// Let the thread sleep for a while.
Thread.sleep(50);
}
catch (InterruptedException e) {
System.out.println("Transaction " + id + " interrupted.");
}
System.out.println("Transaction " + id + " exiting.");
}
public void start ()
{
System.out.println("Starting Transaction " + id );
while(!(from.lock==0 && to.lock==0));
if(t==null)
{
t = new Thread(this,this.id);
t.start ();
}
}
}
public class twophase
{
public static void main(String args[])
{
Account A=new Account("A",500,0);
Account B=new Account("B",800,0);
Account C=new Account("C",900,0);
Transaction T1 = new Transaction( "T-1",A,B,200);
T1.start();
Transaction T2 = new Transaction( "T-2",C,B,500);
T2.start();
Transaction T3 = new Transaction( "T-3",A,B,400);
T3.start();
}
}
*******************OUTPUT*********************
aiktc@aiktc17 ~ $ javac twophase.java
aiktc@aiktc17 ~ $ java twophase
Starting Transaction T-1
Starting Transaction T-2
Running TransactionT-1
Starting Transaction T-3
Transaction T-1 is completed
Running TransactionT-2
Balance of Account A is 300
Transaction T-2 is completed
Balance of Account C is 400
Balance of Account B is 1500
Balance of Account B is 1500
Running TransactionT-3
Transaction Cannot proceed : Not Enough Balance in AccountA
No comments:
Post a Comment