Can I pass a Method as parameter of another method in java?

前端 未结 6 503
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 21:04

I am trying to measure the execution time for several methods. so I was thinking to make a method instead of duplicate same code many times.

Here is my code:

6条回答
  •  长发绾君心
    2021-01-04 21:34

    I have a object call Counter which basically looks like this:

    private long count;
    private long errors;
    private long duration;
    private long errorDuration;
    

    With two "time" methods for timing methods that return something and other methods which are void.

    public  T time(ReturningRunnable runnable) {
        long start = currentTimeMillis();
        T result = null;
        try {
            result = runnable.run();
        } catch (Throwable ex) {
            errorDuration += currentTimeMillis() - start;
            errors++;
            throw runtime(ex);
        }
        duration += currentTimeMillis() - start;
        count++;
        return result;
    }
    
    public void time(Runnable runnable) {
        time(new RunnableRunner(runnable));
    }
    

    I chose to rethrow the exceptions as runtime exceptions (as i'm not a fan of checked exceptions) but you could just as well make your custom MyRunnable interface throw exceptions and just catch and re-throw them. Here's a usage of the above:

    counter.time(new Runnable() {
        @Override
        public void run() {
            // Do something
        });
    

    Or this, in the returning-value case:

    return counter.time(new ReturningRunnable() {
        @Override
        public Integer run() {
            return 1; // You get the idea
        });
    

    I like this because my counter objects can be exposed over JMX and injected wherever i need them. You could do what you've asked with reflection, but i think that would be messy (IMHO).

提交回复
热议问题