zoukankan      html  css  js  c++  java
  • 线程阻塞释放的5种方法

    1 LockSupport类

    • 底层使用Unsafe类
    private static void block1(Thread thread) {
        new Thread(() -> {
            LockSupport.parkNanos(THREE_SECOND);
            LockSupport.unpark(thread);
        }).start();
        LockSupport.park();
    }
    

    2 Unsafe类

    private static void block2(Thread thread) {
        try {
            Field f = Unsafe.class.getDeclaredField("theUnsafe");
            f.setAccessible(true);
            Unsafe unsafe = (Unsafe) f.get(null);
            new Thread(() -> {
                LockSupport.parkNanos(THREE_SECOND);
                unsafe.unpark(thread);
            }).start();
            unsafe.park(false, 0L);
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    

    3 Object类

    private static void block3(Thread thread) {
        new Thread(() -> {
            LockSupport.parkNanos(THREE_SECOND);
            synchronized (thread) {
                thread.notify();
            }
        }).start();
    
        synchronized (thread) {
            try {
                thread.wait(); // 释放锁
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    

    4 CountDownLatch类

    private static void block4() {
        CountDownLatch latch = new CountDownLatch(1);
        new Thread(() -> {
            LockSupport.parkNanos(THREE_SECOND);
            latch.countDown();
        }).start();
    
        try {
            latch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    

    5 自旋

    private static void block5() {
        AtomicBoolean flag = new AtomicBoolean(true);
        new Thread(() -> {
            LockSupport.parkNanos(THREE_SECOND);
            flag.set(false);
        }).start();
        while (flag.get()) {
        }
    }
    

    6 测试

    • 测试误差排行(非准确测试): 自旋 < Unsafe < CountDownLatch < Object < LockSupport
    public class Main {
        private static final long THREE_SECOND = 3_000_000_000L;
        private static final int times = 10;
    
        public static void main(String[] args) {
            System.out.println(System.currentTimeMillis() + ":start");
            Thread thread = Thread.currentThread();
            long diff = 0;
            for (int i = 0; i < times; i++) {
                long start = System.nanoTime();
                block1(thread);//4364820
                //block2(thread);//3881931
                //block3(thread);//4358189
                //block4(); // 4309549
                //block5(); // 3473868
                diff += Math.abs(System.nanoTime() - start - THREE_SECOND);
            }
            System.out.println("block diff Time:"+diff / times);
            System.out.println(System.currentTimeMillis() + ":end");
        }
    }
    
  • 相关阅读:
    web安全记录
    jquery uploadify插件多文件上传
    在java中生成二维码,并直接输出到jsp页面
    条形码/二维码之开源利器ZXing图文介绍
    FCKeditor插件开发实例:uploadify多文件上传插件
    FCKeditor 插件开发 示例
    Spring技术_邮箱注册_激活_获取验证码
    jquery的uploadify上传jsp+servlet
    PHP变量类型
    thinkphp环境变量配置
  • 原文地址:https://www.cnblogs.com/linzhanfly/p/9866543.html
Copyright © 2011-2022 走看看