TimeLimiter tl = new TimeLimiter(10);
在函数入口调用 tl.invoke();就行了
public class TimeLimiter
{
private int cps;
private java.util.Vector times = new java.util.Vector();
public TimeLimiter(int _cps)
{
cps = _cps;
}
public synchronized void invoke()
{
long t = System.currentTimeMillis();
if (times.size()==cps)
{
long t0 = ((Long)times.remove(0)).longValue();
long w = 1000L-(t-t0);
if(w>=0)
{
try
{
Thread.sleep(w);
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
}
times.addElement(new Long(t));
}
}