生活随笔
收集整理的這篇文章主要介紹了
java杂乱
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
為什么80%的碼農(nóng)都做不了架構(gòu)師?>>> ??
1 時(shí)間戳
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結(jié)尾,且需要是final的,其中的方法必須是static的 3 四舍五入保留兩位小數(shù)
DecimalFormat df = new DecimalFormat("#.00");
return Double.valueOf(df.format(valueOfTps / constant));
or
public 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 這個(gè)方法的意思就是在jvm中增加一個(gè)關(guān)閉的鉤子,當(dāng)jvm關(guān)閉的時(shí)候,會(huì)執(zhí)行系統(tǒng)中已經(jīng)設(shè)置的所有通過方法addShutdownHook添加的鉤子,當(dāng)系統(tǒng)執(zhí)行完這些鉤子后,jvm才會(huì)關(guān)閉。所以這些鉤子可以在jvm關(guān)閉的時(shí)候進(jìn)行內(nèi)存清理、對(duì)象銷毀等操作。public class TestShutdownHook {/*** @param args*/public static void main(String[] args) {// 定義線程1Thread thread1 = new Thread() {public void run() {System.out.println("thread1...");}};// 定義線程2Thread thread2 = new Thread() {public void run() {System.out.println("thread2...");}};// 定義關(guān)閉線程Thread shutdownThread = new Thread() {public void run() {System.out.println("shutdownThread...");}};// jvm關(guān)閉的時(shí)候先執(zhí)行該線程鉤子Runtime.getRuntime().addShutdownHook(shutdownThread);thread1.start();thread2.start();}
}打印結(jié)果:thread2...
thread1...
shutdownThread...或者:thread2...
thread1...
shutdownThread...
轉(zhuǎn)載于:https://my.oschina.net/112612/blog/1594970
總結(jié)
以上是生活随笔為你收集整理的java杂乱的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。