class Bank
{
private int sum;
//Object obj=new Object();
public synchronized void add(int n)
{
//synchronized(obj)
//{
sum=sum + n;
try
{
Thread.sleep(10);
}
catch (Exception e)
{
}
System.out.println("sum:"+sum);
//}
}
}
class cus implements Runnable
{
privateBank b=new Bank();
publicvoid run()
{
for(int x=0;x<3 ;x++ )
{
b.add(100);
}
}
}
class BankDemo
{
publicstatic void main(String[] args)
{
cusc=new cus();
ThreadT1=new Thread();
ThreadT2=new Thread();
T1.start();
T2.start();
}
}
|