关键字

  • strictfp

    简介: strictfp, 即 strict float point (精确浮点)。在像x87这样的平台上,溢出和下溢可能不会发生在预期的位置,从而产生可能更有意义但更不可重复的结果。如果你想让你的浮点运算更加精确,而且不会因为不同的硬件平台所执行的结果不一致的话,可以用关键字strictfp。strictfp修饰符可用于确保在所有平台上的相同位置发生溢出和下溢。 strictfp关键字可应用于类、接口、方法。 使用 strictfp 关键字声明一个方法时,该方法中所有的float和double表达式都严格遵守FP-strict的限制,符合IEEE-754规范。当对一个类或接口使用 strictfp 关键字时,该类中的所有代码,包括嵌套类型中的初始设定值和代码,都符合上述规范。

示例:

  1. public strictfp class BasicTest09 {
  2. private static double aDouble = 0.0555500333333212d;
  3. private static float aFloat = 0333000000222f;
  4. public static void main(String[] args) {
  5. double cDouble = aDouble / aFloat;
  6. System.out.println("aDouble:" + aDouble);
  7. System.out.println("aFloat:" + aFloat);
  8. System.out.println("cDouble:" + cDouble);
  9. }
  10. }

语法

  • 判断奇偶

    1. public static boolean oddOrNot(int num) {
    2. return num % 2 == 1;
    3. }
    4. public static boolean oddOrNot(int num) {
    5. return (num & 1) == 1;
    6. }
  • 整数定义

    增强数字可读性

  1. Integer iValue = 1000_000_000;
  2. Long lValue = 1_0000_0000_0000L;
  • 随机数

    java.util.Random
    自1.0就已经存在,是一个线程安全类,理论上可以通过它同时在多个线程中获得互不相同的随机数,这样的线程安全是通过AtomicLong实现的。
    Random使用AtomicLong CAS(compare and set)操作来更新它的seed,CAS在资源高度竞争时的表现糟糕。

    java.util.concurrent.ThreadLocalRandom 多线程通过ThreadLocalRandom的current方法获取ThreadLocalRandom的实例时候其实获取的是同一个,每个线程维护自己的一个种子变量,所以ThreadLocalRandom的实例里面只是与线程无关的通用算法,所以是线程安全的。每个线程生成随机数时候根据自己老的种子计算新的种子,并使用新种子更新老的种子,然后根据新种子计算随机数,就不会存在竞争问题,这会大大提高并发性能。

    public static ThreadLocalRandom current() {
        if (UNSAFE.getInt(Thread.currentThread(), PROBE) == 0)
            localInit();
        return instance;
    }

注解

  • @SneakyThrows

    一点点简化代码的作用。

    public void upStream() {
        try {
            downstream("", "");
        } catch (IOException e) {
            System.out.println("自定义处理");
        } catch (InterruptedException e) {
            System.out.println("自定义处理");
        } catch (ClassNotFoundException e) {
            System.out.println("自定义处理");
        }
    }

    public static void downstream(String filePath, String target) throws IOException, ClassNotFoundException, InterruptedException {
        List<String> list = Files.readLines(new File(filePath), Charset.forName("GBK"));
        for (int i = 0; i < list.size(); i++) {
            if (!Objects.equals(list.get(i), target)) {
                Class<?> aClass = Class.forName(list.get(i));
                String fileName = aClass.getName();
                System.out.println(fileName);
                Thread.sleep(100L);
            }
        }
    }
    public void upStream() {
        try {
            downstream("", "");
        } catch (Exception e) {
            System.out.println("自定义处理");
        }
    }

    @SneakyThrows({InterruptedException.class, ClassNotFoundException.class})
    public static void downstream(String filePath, String target) {
        List<String> list = null;
        try {
            list = Files.readLines(new File(filePath), Charset.forName("GBK"));
        } catch (IOException e) {
            System.out.println("io 异常就地处理");
        }
        for (int i = 0; i < list.size(); i++) {
            if (!Objects.equals(list.get(i), target)) {
                Class<?> aClass = Class.forName(list.get(i));
                String fileName = aClass.getName();
                System.out.println(fileName);
                Thread.sleep(100L);
            }
        }
    }

它生成的代码加上了try…cache块,并以新的Lombok.sneakyThrow的方式,伪装成运行时异常向上抛出。

        try {

        } catch (Throwable e) {
            throw Lombok.sneakyThrow(e);
        }

        public static RuntimeException sneakyThrow (Throwable t){
            if (t == null) throw new NullPointerException("t");
            return Lombok.<RuntimeException>sneakyThrow0(t);
        }