Java 偏向锁测试
来源:互联网 发布:windows界面编程 编辑:程序博客网 时间:2024/04/29 10:33
JDK 1.8.0_60
import java.util.Random;import java.util.concurrent.*;/** * Created by Administrator on 2016/1/27. * JDK 1.8.0_65 */public class Test5 { private static final int NTHREAD = 4; private static final int COUNTER = 10000000; private static Random random = new Random(123); private static ExecutorService es1 = Executors.newSingleThreadExecutor(); private static ExecutorService es2 = Executors.newFixedThreadPool(NTHREAD); private static ThreadLocal<Random> tl = new ThreadLocal<Random>() { @Override protected Random initialValue() { return new Random(123); } }; private static class Task implements Callable<Long> { private int mode = 0; public Task(int mode) { this.mode = mode; } public Random getRandom() { if (mode == 0) { return random; } else if (mode == 1) { return tl.get(); }else return null; } @Override public Long call() throws Exception { long start = System.currentTimeMillis(); for(long i = 0; i < COUNTER; i++) { getRandom().nextInt(); } long end = System.currentTimeMillis(); System.out.println(Thread.currentThread().getName() + "spend " + (end - start) + " ms"); return (end - start); } } public static void main(String... args) throws ExecutionException, InterruptedException { Future<Long> futs1 = es1.submit(new Task(0)); long time1 = 0; time1 += futs1.get(); System.out.println("+UseBiasedLocking : " + time1 + " ms"); //System.out.println("-UseBiasedLocking : " + time1 + " ms"); es1.shutdown(); Future<Long>[] futs2 = new Future[NTHREAD]; for(int i = 0; i < NTHREAD ; i++) { futs2[i] = es2.submit(new Task(0)); } long time2 = 0; for(int i = 0; i < NTHREAD ; i++) { time2 += futs2[i].get(); } System.out.println("N Thread access one Random instance : " + time2 + " ms"); for(int i = 0; i < NTHREAD ; i++) { futs2[i] = es2.submit(new Task(1)); } long time3 = 0; for(int i = 0; i < NTHREAD ; i++) { time3 += futs2[i].get(); } System.out.println("ThreadLocal access one Random instance : " + time3 + " ms"); es2.shutdown(); }}
//可以明显的看到使用偏向锁,单线程时间减少,但是多线程时间延长
pool-1-thread-1spend 563 ms
+UseBiasedLocking : 563 ms
pool-2-thread-4spend 2121 ms
pool-2-thread-2spend 2130 ms
pool-2-thread-3spend 2309 ms
pool-2-thread-1spend 2310 ms
N Thread access one Random instance : 8870 ms
pool-2-thread-2spend 1563 ms
pool-2-thread-4spend 1714 ms
pool-2-thread-1spend 1603 ms
pool-2-thread-3spend 1784 ms
ThreadLocal access one Random instance : 6664 ms
//可以明显的看到禁用偏向锁,单线程时间延长,但是多线程时间减少
-UseBiasedLocking
pool-1-thread-1spend 760 ms
-UseBiasedLocking : 760 ms
pool-2-thread-1spend 1153 ms
pool-2-thread-3spend 1141 ms
pool-2-thread-4spend 1713 ms
pool-2-thread-2spend 2081 ms
N Thread access one Random instance : 6088 ms
pool-2-thread-1spend 1131 ms
pool-2-thread-3spend 1027 ms
pool-2-thread-4spend 1463 ms
pool-2-thread-2spend 1648 ms
ThreadLocal access one Random instance : 5269 ms
-UseBiasedLocking
pool-1-thread-1spend 760 ms
-UseBiasedLocking : 760 ms
pool-2-thread-1spend 1153 ms
pool-2-thread-3spend 1141 ms
pool-2-thread-4spend 1713 ms
pool-2-thread-2spend 2081 ms
N Thread access one Random instance : 6088 ms
pool-2-thread-1spend 1131 ms
pool-2-thread-3spend 1027 ms
pool-2-thread-4spend 1463 ms
pool-2-thread-2spend 1648 ms
ThreadLocal access one Random instance : 5269 ms
- Java 偏向锁测试
- Java偏向锁
- java 偏向锁
- java虚拟机的偏向锁
- java 偏向锁优化技术
- Java的锁偏置(偏向锁)
- Java偏向锁实现原理(Biased Locking)
- Java偏向锁实现原理(Biased Locking)
- Java偏向锁实现原理(Biased Locking)
- Java偏向锁实现原理(Biased Locking)
- Java偏向锁实现原理(Biased Locking)
- Java偏向锁实现原理(Biased Locking)
- java gc 偏向使用
- Java中的锁机制(偏向锁、轻量级锁、重量级锁)
- 乱弹java并发(七)-- 轻量级锁、偏向锁
- Java多线程优化之偏向锁原理分析(转载)
- Java多线程优化之偏向锁原理分析
- 软件测试人员不能避免偏向,但是可以管理偏向
- Linux-IPC之信号量
- 点击2次Back键退出程序
- DateUtils时间工具类探究
- MD5转换2
- LCIS vijos—P1264
- Java 偏向锁测试
- 上层APP调用底层硬件驱动过程解析
- 《笨办法学Python》 第25课手记
- js 自定义滑块 实现文字滚动+透明度变化
- cf#312-C - Amr and Chemistry-枚举
- break的三种用法
- positive definite quadratic form and positive definite matrix
- orhanobut_logger源码分析
- Coursera课程Python for everyone:Quiz: Regular Expressions