class TeacherAccount{
private int[] momth= new int[13];
private int num=0;
public synchronized void deposit(int mon){
num++;
nonth[num]=mon;
this.notify();
}
public synchronized int withdraw(){
while(num==0){
try{ this.wait();}
catch(InterruptedException e){}
}
nun--;
return month[num+1];
}
}
class SchoolBank implements Runnable{
TeacherAccount account;
public SchoolBank(TeacherAccount s){account=s;}
public void run(){
for(int i=1;i<7;i++){
account.deposit(i);
System.out.println("学校发放:"+i+"月份工资");
try{
Thread.sleep((int)Math.random()*10);
}
catch(InterruptedException e){}
}
}
}
class Teacher implements Runnable{
TeacherAccount account;
public Teacher(TeacherAccount s){account=s;}
public void run(){
int temp;
for(i=1;i<12;i++){
temp=account.withdraw();
System.out.println("教师提取:"+temp+"月份工资");
try{
Thread.sleep((int)Math.random()*10);
}
catch(InterruptedException e){}
}
}
}
public class TeacherAccountTest{
public static void main(String[] args){
TeacherAccount staffaccount=new TeacherAccount();
SchoolBank con=new SchoolBank(staffaccount);
Teacher sta=new Teacher(staffaccount);
Thread t1=new Thread(com);
Thread t2=new Thread(sta);
t1.start();
t2.start();
}
}