支付宝面试题: 30秒内限制某方法被调用100次 (一个时间段限制方法被调用的次数)

假设一个Class的方法 a,被限定30秒内只能被调用100次,如何实现。


当时没答上,回来补上   


1、定义调用接口Icallee


public interface Icallee {


public void play();
}



2、实现调用接口类


public class Callee implements Icallee {


@Override
public void play() {
// TODO Auto-generated method stub
System.out.println(" Hello...The method is being called. ");
}


}


3、限制一段时间内方法调用次数类


public  class CheckCalledTimesInPeriod {


private Icallee callee; 

private  int limitTime;
 
private  long startMills;

private  int limitCount;


// record the times that be called
static private int count = 0 ;  
 
public CheckCalledTimesInPeriod(int minute,long startMills,int limitCount){
       this.limitTime= minute;
       this.startMills = startMills;
       this.limitCount = limitCount;     
       callee = new Callee();
   }
 
//Limit the times that be called for a period
public  synchronized void execute(){
  
    long nowMills = System.currentTimeMillis();  
   
    count++;

    if(nowMills<=(startMills+limitTime*60000)&&count<=limitCount) 
    {
    callee.play();
    }
   
    //to Log the times
    Calendar c = Calendar.getInstance(); 
    DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    CallerRunning.sb.append("You are calling = "+count+" times. The time is = "+format.format(c.getTime())+"\r\n");
System.out.println("You are calling "+count+" times."+"The Time is = "+format.format(c.getTime()));
   
   }
   
}


4、测试运行类



public class CallerRunning {


// to store the log  data  
static public   StringBuffer sb = new StringBuffer();
/**
* @param args
*/
public static void main(String[] args) {
 

        long startMills = System.currentTimeMillis();
    long nowMills = startMills ;
   
    //call a method 60000 times in 1 minute 
    CheckCalledTimesInPeriod called = new CheckCalledTimesInPeriod(1, startMills, 60000);
   
    //to log the running time
    Calendar c = Calendar.getInstance(); 
DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   sb.append("begin time is = "+format.format(c.getTime())+"\r\n");
   
   //test run  in 6 second
    while(nowMills<=(startMills+6000))
    {
   
          nowMills = System.currentTimeMillis();
          called.execute();
    }
   
    //to write the log to txt
    Calendar d = Calendar.getInstance(); 
    sb.append("end time is = "+format.format(d.getTime())+"\r\n");
    writeTxt("C:\\test.txt", sb.toString());
}


public static void writeTxt(String filePath, String content){

       try {  
           File f = new File(filePath);  
           if (!f.exists()) {  
               f.createNewFile();// 不存在则创建  
           }  
           BufferedWriter output = new BufferedWriter(new FileWriter(f));  
           output.write(content);  
           output.close();  
       } catch (Exception e) {  
           e.printStackTrace();  
 
       }  
}


}



5、TXT文件结果 LOG 


begin time is = 2014-04-04 04:21:22
You are calling = 1 times. The time is = 2014-04-04 04:21:22
You are calling = 2 times. The time is = 2014-04-04 04:21:22
You are calling = 3 times. The time is = 2014-04-04 04:21:22
You are calling = 4 times. The time is = 2014-04-04 04:21:22
You are calling = 5 times. The time is = 2014-04-04 04:21:22
You are calling = 6 times. The time is = 2014-04-04 04:21:22
You are calling = 7 times. The time is = 2014-04-04 04:21:22
You are calling = 8 times. The time is = 2014-04-04 04:21:22
You are calling = 9 times. The time is = 2014-04-04 04:21:22
You are calling = 10 times. The time is = 2014-04-04 04:21:22
You are calling = 11 times. The time is = 2014-04-04 04:21:22
You are calling = 12 times. The time is = 2014-04-04 04:21:22
You are calling = 13 times. The time is = 2014-04-04 04:21:22
You are calling = 14 times. The time is = 2014-04-04 04:21:22
You are calling = 15 times. The time is = 2014-04-04 04:21:22
You are calling = 16 times. The time is = 2014-04-04 04:21:22
You are calling = 17 times. The time is = 2014-04-04 04:21:22
You are calling = 18 times. The time is = 2014-04-04 04:21:22
You are calling = 19 times. The time is = 2014-04-04 04:21:22
You are calling = 20 times. The time is = 2014-04-04 04:21:22
You are calling = 21 times. The time is = 2014-04-04 04:21:22
You are calling = 22 times. The time is = 2014-04-04 04:21:22
You are calling = 23 times. The time is = 2014-04-04 04:21:22
You are calling = 24 times. The time is = 2014-04-04 04:21:22
You are calling = 25 times. The time is = 2014-04-04 04:21:22
You are calling = 26 times. The time is = 2014-04-04 04:21:22
You are calling = 27 times. The time is = 2014-04-04 04:21:22
You are calling = 28 times. The time is = 2014-04-04 04:21:22
You are calling = 29 times. The time is = 2014-04-04 04:21:22
You are calling = 30 times. The time is = 2014-04-04 04:21:22
You are calling = 31 times. The time is = 2014-04-04 04:21:22
You are calling = 32 times. The time is = 2014-04-04 04:21:22
You are calling = 33 times. The time is = 2014-04-04 04:21:22
You are calling = 34 times. The time is = 2014-04-04 04:21:22
You are calling = 35 times. The time is = 2014-04-04 04:21:22
You are calling = 36 times. The time is = 2014-04-04 04:21:22
You are calling = 37 times. The time is = 2014-04-04 04:21:22
You are calling = 38 times. The time is = 2014-04-04 04:21:22
You are calling = 39 times. The time is = 2014-04-04 04:21:22
You are calling = 40 times. The time is = 2014-04-04 04:21:22

。。。。。。。。。。。。。。。。。。。。。。。。。。。。



你可能感兴趣的:(面试题,支付宝)