<1000;i++){executorService.submit(new CounterRunnable(counter,10000));}//关闭线程池executorService.shutdown();//等待线程池中任务全部完成 , 最多1个小时executorService.awaitTermination(1, TimeUnit.HOURS);long end = System.currentTimeMillis();System.out.println("Counter result:"+ counter.getCounter());System.out.println("Time passed in ms:"+ (end-start));}interface Counter{void increnment();long getCounter();}static class StupidCounter implements Counter{private long counter = 0;public void increnment(){counter++;}public long getCounter(){return counter;}}static class CounterRunnable implements Runnable{private final Counter counter;private final int num;public CounterRunnable(Counter counter, int num) {this.counter = counter;this.num = num;}@Overridepublic void run() {for (int i=0;i
输出结果:
Counter result:9989619Time passed in ms:210
2、加上锁时
package com.test.unsafe;import sun.misc.Unsafe;import java.lang.reflect.Field;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicLong;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class UnsafeTest {public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InterruptedException {//定义线程池ExecutorService executorService = Executors.newFixedThreadPool(1000);Counter counter = new SyncCounter();//记录开始时间long start = System.currentTimeMillis();//提交1000个任务for (int i=0;i<1000;i++){executorService.submit(new CounterRunnable(counter,10000));}//关闭线程池executorService.shutdown();//等待线程池中任务全部完成 , 最多1个小时executorService.awaitTermination(1, TimeUnit.HOURS);long end = System.currentTimeMillis();System.out.println("Counter result:"+ counter.getCounter());System.out.println("Time passed in ms:"+ (end-start));}interface Counter{void increnment();long getCounter();}static class SyncCounter implements Counter{private long counter = 0;public synchronized void increnment(){counter++;}public long getCounter(){return counter;}}static class CounterRunnable implements Runnable{private final Counter counter;private final int num;public CounterRunnable(Counter counter, int num) {this.counter = counter;this.num = num;}@Overridepublic void run() {for (int i=0;i
输出结果:
Counter result:10000000Time passed in ms:538
3、加上lock锁时
package com.test.unsafe;import sun.misc.Unsafe;import java.lang.reflect.Field;import java.util.concurrent.Executor;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;import java.util.concurrent.atomic.AtomicLong;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;public class UnsafeTest {public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException, InterruptedException {//定义线程池ExecutorService executorService = Executors.newFixedThreadPool(1000);Counter counter = new LockCounter();//记录开始时间long start = System.currentTimeMillis();//提交1000个任务for (int i=0;i<1000;i++){executorService.submit(new CounterRunnable(counter,10000));}//关闭线程池executorService.shutdown();//等待线程池中任务全部完成 , 最多1个小时executorService.awaitTermination(1, TimeUnit.HOURS);long end = System.currentTimeMillis();System.out.println("Counter result:"+ counter.getCounter());System.out.println("Time passed in ms:"+ (end-start));}interface Counter{void increnment();long getCounter();}static class LockCounter implements Counter{private long counter = 0;private final Lock lock = new ReentrantLock();public void increnment(){try{lock.lock();counter++;}finally {lock.unlock();}}public long getCounter(){return counter;}}static class CounterRunnable implements Runnable{private final Counter counter;private final int num;public CounterRunnable(Counter counter, int num) {this.counter = counter;this.num = num;}@Overridepublic void run() {for (int i=0;i
- 能够同时进出车 可直接运行的基于Java多线程的停车场管理系统
- MindMaze要把VR治疗系统带到美国,专攻中风康复治疗
- 2021中国数字智能生态大会,易趋布局项目管理生态合作
- QT窗体绘图QPainter
- BZOJ 1951 浅谈猪王国古代文字及中国剩余定理合并半拓展LuCas
- vagrant使用_使用Vagrant的Kubernetes上的Java EE
- 7 java并发编程实战 并发工具JUC之CountDownLatch
- 中国温度带分布图高清 中国温度带分布高清图
- android 数据库 自动生成,android开发中sqlite代码生成器
- CSS中的 “var” 和 “:root”