zoukankan      html  css  js  c++  java
  • 【ThreadLocal】使用ThreadLocal实现线程安全

    非线程安全

    public class UnSafeThreadLocalDemo {
        private int count = 0;
    
        public static void main(String[] args) {
            UnSafeThreadLocalDemo unSafeThreadLocalDemo = new UnSafeThreadLocalDemo();
            for (int i = 0; i < 5; i++) {
                int finalI = i;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        unSafeThreadLocalDemo.process();
                        unSafeThreadLocalDemo.print(finalI);
                    }
                }).start();
            }
    
        }
    
        public void process() {
            for (int i = 0; i < 10; i++) {
                count += 1;
                try {
                    TimeUnit.MILLISECONDS.sleep(new Random().nextInt(10) + 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        public void print(int i) {
            System.out.println("count(" + i + ") = " + count);
        }
    }
    

    输出:

    count(1) = 44
    count(0) = 46
    count(3) = 47
    count(2) = 49
    count(4) = 49
    

    线程安全

    public class SafeThreadLocalDemo {
        //    private int count = 0;
        private ThreadLocal<Integer> count = new ThreadLocal<Integer>() {
            protected Integer initialValue() {
                return 0;
            }
        };
    
        public void process() {
            for (int i = 0; i < 10; i++) {
                count.set(count.get() + 1);
                try {
                    TimeUnit.MILLISECONDS.sleep(new Random().nextInt(10) + 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
    
        }
    
        public void print(int i) {
            System.out.println("count(" + i + ") = " + count.get());
        }
    
        public static void main(String[] args) {
            SafeThreadLocalDemo safeThreadLocalDemo = new SafeThreadLocalDemo();
            for (int i = 0; i < 5; i++) {
                int finalI = i;
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        safeThreadLocalDemo.process();
                        safeThreadLocalDemo.print(finalI);
                    }
                }).start();
            }
    
        }
    }
    

    输出:

    count(1) = 10
    count(0) = 10
    count(2) = 10
    count(4) = 10
    count(3) = 10
    
  • 相关阅读:
    航班预定统计(差分数组+前缀和)
    救生艇
    Xcode 的正确打开方式——Debugging
    多次页面跳转后pop回主界面的问题
    理解Bitcode:一种中间代码
    使用AFNetWorking读取JSON出现NSCocoaErrorDomain Code=3840的解决方法
    No identities are available for signing的解决方法
    Aufree/trip-to-iOS
    Alcatraz -- 一个神奇的管理插件的Xcode插件
    GenericKeychain
  • 原文地址:https://www.cnblogs.com/ssslinppp/p/8036903.html
Copyright © 2011-2022 走看看