线程安全性--synchronized

synchronized做用范围

  • 修饰代码块:修饰范围是大括号括起来的代码,做用于调用的对象
  • 修饰方法:修饰范围是整个方法,做用于调用对象
  • 修饰静态方法:修饰范围是整个静态方法,做用于全部对象
  • 修饰类:修饰范围是括号括起来的部分,做用于全部对象
public class SynchronizedBlock {

    public void test(String threadName){
        synchronized (this){
            for (int i=0;i<10;i++){
                System.out.println("test--:"+threadName+"  "+i);
            }
        }
    }

    public synchronized void test2(){
        for (int i=0;i<10;i++){
            System.out.println("test2-i:"+i);
        }
    }

    public static void main(String[] args) {
        final SynchronizedBlock syn1 = new SynchronizedBlock();
        final ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn1.test(Thread.currentThread().getName());
            }
        });

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn1.test(Thread.currentThread().getName());
            }
        });
    }
}

上述代码的运行结果如预期,两个线程按顺序输出0-9ide

public static void main(String[] args) {
        final SynchronizedBlock syn1 = new SynchronizedBlock();
        final SynchronizedBlock syn2 = new SynchronizedBlock();
        final ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn1.test(Thread.currentThread().getName());
            }
        });

        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn2.test(Thread.currentThread().getName());
            }
        });
    }

若是是两个实例,运行结果又会是怎样呢?this

屡次的运行结果都不相同。能够看出,synchronized修饰代码块和非静态方法时,锁定的对象都只是当前调用对象。线程

public class SynchronizedBlock {

    public void test(String threadName){
        synchronized (SynchronizedBlock.class){
            for (int i=0;i<10;i++){
                System.out.println("test--:"+threadName+"  "+i);
            }
        }
    }

    public synchronized static void test2(String threadName){
        for (int i=0;i<10;i++){
            System.out.println("test2--:"+threadName+"  "+i);
        }
    }

    public static void main(String[] args) {
        final SynchronizedBlock syn1 = new SynchronizedBlock();
        final SynchronizedBlock syn2 = new SynchronizedBlock();
        final ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn1.test2(Thread.currentThread().getName());
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn2.test2(Thread.currentThread().getName());
            }
        });
    }
}

当synchronized修饰类和静态方法时,锁定的对象是这个类的全部实例。code

对于同一个实例,访问该实例被synchronized修饰的两个方法,会被阻塞吗?对象

@Slf4j
public class SynchronizedBlock2 {

    public synchronized void test2() {
        log.info("start test2---------------");
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("test2-----------");
    }

    public synchronized void test3() {
        for (int i = 0; i < 10; i++) {
           log.info("test3--:{}  " + i);
        }
    }

    public static void main(String[] args) {
        final SynchronizedBlock2 syn1 = new SynchronizedBlock2();
        final ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn1.test2();
            }
        });
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                syn1.test3();
            }
        });
    }
}

答案是。。。。。。。。。。。。固然会blog

个人理解是这样的,对于同一个实例来讲,访问test2方法时,至关于给它加了锁,只有test2执行完,才能释放锁继续执行test3。也不知道对不对,欢迎指正。get

相关文章
相关标签/搜索