74.sychronization in java
//sychronization
class Account
{
static int bal;
Account()
{
bal = 0;
}
synchronized void deposit(int amt)
{
bal = bal + amt;
}
}
OUTPUT:
Balance : 1000000
class customer extends Thread
{
Account act;
customer (Account ac)
{
act = ac;
}
public void run()
{
for (int i = 1; i <= 10000; i++) {
act.deposit(10);
}
}
}
class sychronization
{
public static void main(String[] args) {
Account acc = new Account();
customer cust[] = new customer[10];
int a;
for (a = 0; a < 10; a++) {
cust[a] = new customer(acc);
cust[a].start();
}
for (a = 0; a < 10; a++) {
try{
cust[a].join();
}
catch(Exception e)
{
System.out.println("Exception : " + e);
}
}
System.out.println("Balance : " + acc.bal);
}
}
Comments
Post a Comment