java杂乱

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));
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);
    }
  1. 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...

转载于:https://my.oschina.net/112612/blog/1594970

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值