1 时间戳
long timeOfBefore=System.currentTimeMillis(); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } long timeOfAfter=System.currentTimeMillis(); System.out.println(timeOfAfter); System.out.println(timeOfBefore); System.out.println(timeOfAfter-timeOfBefore);
2 工具类以类名Utils结尾,且需要是final的,其中的方法必须是static的 3 四舍五入保留两位小数
DecimalFormat df = new DecimalFormat("#.00");return Double.valueOf(df.format(valueOfTps / constant));orpublic static String saveTwoPoint(double d) { return savePoint(d, 2); } public static String savePoint(double d, int point) { return String.format("%.".concat(point + "f"), d); }
- RunTime.getRunTime().addShutdownHook 这个方法的意思就是在jvm中增加一个关闭的钩子,当jvm关闭的时候,会执行系统中已经设置的所有通过方法addShutdownHook添加的钩子,当系统执行完这些钩子后,jvm才会关闭。所以这些钩子可以在jvm关闭的时候进行内存清理、对象销毁等操作。
public class TestShutdownHook { /** * @param args */ public static void main(String[] args) { // 定义线程1 Thread thread1 = new Thread() { public void run() { System.out.println("thread1..."); } }; // 定义线程2 Thread thread2 = new Thread() { public void run() { System.out.println("thread2..."); } }; // 定义关闭线程 Thread shutdownThread = new Thread() { public void run() { System.out.println("shutdownThread..."); } }; // jvm关闭的时候先执行该线程钩子 Runtime.getRuntime().addShutdownHook(shutdownThread); thread1.start(); thread2.start(); }}打印结果:thread2...thread1...shutdownThread...或者:thread2...thread1...shutdownThread...