同步方法和同步块有什么区别?
synchronized
方法和synchronized
语句有什么区别?
如果可以的话,请用一个例子来让它更清楚。
What is the difference between synchronized
methods and synchronized
statements?
If possible, please use an example to make it more clear.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
同步方法会锁定与类实例(即“this”)或类(如果是静态方法)关联的监视器,并阻止其他人这样做,直到从该方法返回为止。同步块可以锁定任何监视器(您告诉它哪个),并且可以具有比封闭方法更小的范围。
如果同步块最终不等于方法的整个范围,和/或如果它们锁定的东西比实例(或类,如果是静态的)不那么严格,则首选同步块。
A synchronized method locks the monitor associated with the class instance (ie 'this') or the class (if a static method), and prevents others from doing so until the return from the method. A synchronized block can lock any monitor (you tell it which) and can have a scope smaller than that of the enclosing method.
Synchronized blocks are preferred if they don't end up equivalent to the entire scope of the method, and/or if they lock something less draconian than the instance (or class if static).
引自 JLS(包括示例):
JLS 14.19
同步
声明JLS 8.4.3.6 <代码>同步方法
那么它们有何不同?
引自Effective Java 2nd Edition,第 67 项:避免过度同步:
方法的
synchronized
修饰符作为一种语法糖,适用于许多但并非所有场景。本书更深入地讨论了为什么应该避免过度同步,但基本上通过使用同步语句,您可以更好地控制同步区域的边界(如果场景需要,也可以选择自己的锁)。除非您的方法非常简单和/或您需要在方法的整个持续时间内获取
this
锁(或者如果方法是则获取
),您应该使用Class
对象锁) staticsynchronized
语句将方法内的同步限制为仅在您需要时(即,当您访问共享可变数据时)。Quotes from the JLS (including example):
JLS 14.19 The
synchronized
StatementJLS 8.4.3.6
synchronized
MethodsSo how are they different?
A quotes from Effective Java 2nd Edition, Item 67: Avoid excessive synchronization:
The
synchronized
modifier for methods, being a syntactic sugar that it is, is applicable in many but not all scenarios. The book goes to discuss in much deeper detail why you should avoid excessive synchronization, but basically by usingsynchronized
statements, you have much greater control over the boundaries of yoursynchronized
regions (and if the scenario requires it, you can also choose your own locks).Unless your method is very simple and/or you need to acquire the
this
lock for the entire duration of the method (or theClass
object lock if the method isstatic
), you should usesynchronized
statements to limit the synchronization within the method to only to when you need it (i.e. when you're accessing shared mutable data).synchronized
方法是其主体自动封装在synchronized
块中的方法。因此,这是相等的:
A
synchronized
method is a method whose body is encapsulated automatically in asynchronized
block.Thus, this is equal:
synchronized
on 方法锁定在this
对象上。它等于synchronized (this) {}
标准
synchronized
锁定在指定的对象/监视器上。通过synchronized (***) {}
,您可以选择用于锁定的对象。synchronized
on method is locked on athis
object. It's equals tosynchronized (this) {}
Standard
synchronized
is locked on specified object/monitor. Withsynchronized (***) {}
you can choose an object to use for locking.实际上,同步方法是一种将函数的整个主体放置在同步块中的方法。同步块的优点是您可以将同步块仅应用于函数中的几个 select 语句,而不是应用于整个函数。一般来说,最好使同步块尽可能短,因为花费在同步块中的时间可能会阻止其他线程执行有意义的工作。另一个区别是,在使用同步块时,您可以指定要应用锁的特定对象,而使用同步方法时,对象本身会自动用作执行同步的锁。
A synchronized method is one where you have, in effect, placed the entire body of the function in a synchronized block. A synchronized block has the advantage that you can apply the synchronized block to just a few select statements in the function, instead of to the function as a whole. In general, it is best to make synchronized blocks as short as possible, since time spent in synchronized blocks is time that some other thread might be prevented from doing meaningful work. Another distinction is that you can specify a particular object on which to apply the lock when using a synchronized block whereas with a synchronized method, the object, itself is automatically used as the lock on which synchronization is performed.