1、
Java计时器接口,实现抽象方法,将所要执行的任务放到Task中,再传入到test方法中进行计时测试
/** *作者:魏宝航 *2020年11月23日,下午23:10 */ package 测试; import java.text.SimpleDateFormat; import java.util.Date; public class Times { private static final SimpleDateFormat fmt = new SimpleDateFormat("HH:mm:ss.SSS"); public interface Task { void execute(); } public static void test(String title, Task task) { if (task == null) return; title = (title == null) ? "" : ("【" + title + "】"); System.out.println(title); System.out.println("开始:" + fmt.format(new Date())); long begin = System.currentTimeMillis(); task.execute(); long end = System.currentTimeMillis(); System.out.println("结束:" + fmt.format(new Date())); double delta = (end - begin) / 1000.0; System.out.println("耗时:" + delta + "秒"); System.out.println("-------------------------------------"); } }