码迷,mamicode.com
首页 > 其他好文 > 详细

Unsafe在一个例子

时间:2019-11-10 13:53:40      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:lis   final   span   atom   alac   内存   his   read   etc   

Java和C++语言的一个重要区别就是Java中我们无法直接操作一块内存区域,不能像C++中那样可以自己申请内存和释放内存。Java中的Unsafe类为我们提供了类似C++手动管理内存的能力,不建议使用该类

public class UnsafeTest {

    public static void main(String[] args) throws Exception {
        ExecutorService service = Executors.newFixedThreadPool(1000);
        Counter counter = new CASCounter();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            service.submit(new CounterRunnable(counter, 10000));
        }
        service.shutdown();
        service.awaitTermination(1, TimeUnit.HOURS);
        System.out.println("counter : " + counter.getCount());
        System.out.println("time elapse : " + (System.currentTimeMillis() - start));

    }

    public static Unsafe getUnsafe() throws IllegalAccessException {
        Field unsafeField = Unsafe.class.getDeclaredFields()[0];
        unsafeField.setAccessible(true);
        Unsafe unsafe = (Unsafe) unsafeField.get(null);
        return unsafe;
    }

    interface Counter {
        void increment();

        long getCount();
    }

    static class StupidCounter implements Counter {

        private long value = 0;

        @Override
        public void increment() {
            value++;
        }

        @Override
        public long getCount() {
            return value;
        }

    }

    static class SynCounter implements Counter {

        private long value = 0;

        @Override
        public synchronized void increment() {
            value++;
        }

        @Override
        public long getCount() {
            return value;
        }

    }

    static class LockCounter implements Counter {

        private long value = 0;

        private Lock lock = new ReentrantLock();

        @Override
        public void increment() {
            lock.lock();
            try {
                value++;
            } finally {
                lock.unlock();
            }
        }

        @Override
        public long getCount() {
            return value;
        }

    }

    static class AtomicCounter implements Counter {

        private AtomicLong value = new AtomicLong();

        @Override
        public void increment() {
            value.incrementAndGet();
        }

        @Override
        public long getCount() {
            return value.get();
        }

    }

    static class CASCounter implements Counter {

        private Unsafe unsafe;
        private long offset;
        private volatile long value = 0;

        CASCounter() throws Exception {
            unsafe = getUnsafe();
            offset = unsafe.objectFieldOffset(CASCounter.class.getDeclaredField("value"));
        }

        @Override
        public void increment() {
            long current = value;
            while (!unsafe.compareAndSwapLong(this, offset, value, value + 1)) {
                current = value;
            }
        }

        @Override
        public long getCount() {
            return value;
        }

    }

    static class CounterRunnable implements Runnable {
        Counter counter;
        int num;

        CounterRunnable(Counter counter, int num) {
            this.counter = counter;
            this.num = num;
        }

        @Override
        public void run() {
            for (int i = 0; i < num; i++) {
                counter.increment();
            }
        }
    }

}

 

Unsafe在一个例子

标签:lis   final   span   atom   alac   内存   his   read   etc   

原文地址:https://www.cnblogs.com/moris5013/p/11829412.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!