被同步和lambda表达式混淆

时间:2019-05-14 14:31:38

标签: java

我对这些代码感到困惑。

@Slf4j
public class SynchronizedExample1 {


    public void test1(int x){
        synchronized (this){
            for (int i = 0; i < 100; i++) {
                log.info("test1 -{} - {}",x,i);
            }
        }
    }


    public static void main(String[] args) {
        SynchronizedExample1 synchronizedExample1 = new SynchronizedExample1();
        SynchronizedExample1 synchronizedExample2 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(() -> {
                    synchronizedExample1.test1(1);
                }
        );
        executorService.execute(() -> {
                    synchronizedExample2.test1(2);
                }
        );
    }
}

事实不安全,对吧?因为它们是不同的参考权?
控制台日志:

22:24:55.642 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 0
22:24:55.642 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 0
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 1
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 1
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 2
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 3
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 2
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 4
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 3
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 5
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 4
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 6
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 5
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 7
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 8
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 6
22:24:55.647 [pool-1-thread-2] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 9
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 7
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 8
22:24:55.647 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 9

但是如果我使用正则表达式

public class SynchronizedExample1 {


    //修饰一个代码块
    public void test1(int x){
        synchronized (this){
            for (int i = 0; i < 10; i++) {
                log.info("test1 -{} - {}",x,i);
            }
        }
    }


    public static void main(String[] args) {
        SynchronizedExample1 synchronizedExample1 = new SynchronizedExample1();
        SynchronizedExample1 synchronizedExample2 = new SynchronizedExample1();
        ExecutorService executorService = Executors.newCachedThreadPool();
        executorService.execute(new Runnable() {
            @Override
            public void run() {
                synchronizedExample1.test1(1);
            }
        });

        executorService.execute(()->{
            synchronizedExample2.test1(2);
        });
    }
}

控制台日志:

22:28:15.412 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 0
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 1
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 2
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 3
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 4
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 5
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 6
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 7
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 8
22:28:15.416 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -1 - 9
22:28:15.453 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 0
22:28:15.453 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 1
22:28:15.453 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 2
22:28:15.453 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 3
22:28:15.453 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 4
22:28:15.453 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 5
22:28:15.454 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 6
22:28:15.454 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 7
22:28:15.454 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 8
22:28:15.454 [pool-1-thread-1] INFO com.concurrency.demo.sync.SynchronizedExample1 - test1 -2 - 9

为什么安全?我只是改变写作方式

我的意思是第二个演示我认为这不安全。但是,为什么控制台看起来很安全?原因:它们是不同的引用

0 个答案:

没有答案