如何创建计时器功能?

mih*_*jlv 2 java function timer

func a(x, y z)想要时间,有没有办法:

time = timeit( a(x,y,z) ) 在java中,它会接受任何函数的时间.

喜欢:

long timeit(Object func){
    long startTime = System.currentTimeMillis();
    func(...);
    return System.currentTimeMillis() - startTime;
}
Run Code Online (Sandbox Code Playgroud)

sas*_*724 5

您需要Runnablepublic void run()方法中将您的函数放在interface中.修改后的代码

public long timeit(Runnable func)
{
    long startTime = System.currentTimeMillis();
    func.run();
    return System.currentTimeMillis() - startTime;
}
Run Code Online (Sandbox Code Playgroud)

功能:

public class MyFunction implements Runnable
{
    @Override public void run() { System.out.println(); }
}
Run Code Online (Sandbox Code Playgroud)