//题目:夫妻俩人去银行存钱,每次存一百。只存三次
class Java
{
public static void main(String [] args)
{
bank b=new bank();
Thread t=new Thread(b);
Thread t1=new Thread(b);
t.start();
t1.start();
}
}
class bank implements Runnable//因为这是实现的接口,接口本来里面的方法都是静态啊
{ M m=new M();
public void run(){
for(int x=0;x<3;x++){
m.add(100);
}
}
}
class M
{
private int num;
public void add(int money){
synchronized(this){
num=num+money;
try
{
Thread .sleep(10);
}
catch (Exception e)
{
}
System.out.println (Thread. currentThread().getName()+"=========="+num);
}
}
}
|