What does "synchronize" exactly do?

This question already has an answer here:

  • What does 'synchronized' mean? 15 answers

  • If T1 gets a lock on method do() ie method is under synchronized block. and other portion of program say method display() is not synchronized then other threads can access this method. So your or is correct.


    Straight from the Java documentation:

    It is not possible for two invocations of synchronized methods on the same object to interleave. When one thread is executing a synchronized method for an object, all other threads that invoke synchronized methods for the same object block (suspend execution) until the first thread is done with the object.

    Therefore, your latter explanation is correct.


    同步方法确保不会同时为多个对象实例调用此方法,并且在执行同步方法期间,所有相关的实例变量在开始执行方法之前都会刷新。

    链接地址: http://www.djcxy.com/p/91856.html

    上一篇: 这段代码在做什么

    下一篇: 什么“同步”到底如何?