双重校验锁为什么要判断两次

  今天写synchronized用例的时候,两个线程共享一个对象数据,当操作i的时候,在同步代码块外面判断了一次i<100,但是每一次跑,都会出现i=100,的情况,此时我想起了单例模式的双重校验锁,为什么要判断两次呢?因为可能出现线程1和线程2,在i=99的时候,同时判断了一次,都进到了for循环里面,此时线程1进入同步代码块,线程2进如阻塞队列,当线程1跑出代码块后,线程2进入同步代码块,线程1对i进行加一操作后,i变成了100,线程2输出100,所以要在同步代码块中再加一次判断,判断i的值

  synchronized代码块使用起来比synchronized方法要灵活得多。因为也许一个方法中只有一部分代码只需要同步,如果此时对整个方法用synchronized进行同步,会影响程序执行效率。而使用synchronized代码块就可以避免这个问题,synchronized代码块可以实现只对需要同步的地方进行同步

import java.util.ArrayList;

public class SynchronizedTest2 {
    public static void main(String[] args) throws InterruptedException {
        SychronizedTest2 synchronized2 = new SychronizedTest2();
        Data data = synchronized2.new Data();
        new Thread(new Runnable() {
            @Override
            public void run() {
                data.insert();
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                data.insert();
            }
        }).start();
        Thread.currentThread().sleep(3000);
    }
    class Data{
        private ArrayList<Integer> arr = new ArrayList<>();
        private int i = 0;
        public void insert(){
            for(;i<100;i ++)
                synchronized(this){
                    if(i<100){
                        if(!arr.contains(i)){
                            System.out.println(Thread.currentThread().getName()+" 正在插入"+i);
                            arr.add(i);
                        }
                    }
                }
            }
        }
    }

  

原文地址:https://www.cnblogs.com/ubuntu1/p/9262792.html