Java中邪恶的Unsafe,一半天使一半魔鬼-刘宇( 四 )

<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