[英]Multi threading lock methods
Ex 1)例 1)
public void addName(String name) {
synchronized(this) {
lastName = name;
nameCount++;
}
nameList.add(name);
}
Ex 2)例 2)
public class MsLunch {
private long c1 = 0;
private long c2 = 0;
private Object lock1 = new Object();
private Object lock2 = new Object();
public void inc1() {
synchronized(lock1) {
c1++;
}
}
public void inc2() {
synchronized(lock2) {
c2++;
}
}
}
I got these two java code from java website but I don't clearly see the difference between these two.我从 java 网站获得了这两个 java 代码,但我不清楚这两者之间的区别。 Could you guys give more explanation which case I should use this interchangeably?
你们能否给出更多解释我应该互换使用哪种情况? Thanks in advance
提前致谢
There are two differences:有两个区别:
this
instead of a private reference.this
而不是私有引用。 I view this as a bad idea in general - it means other code could also lock on the same monitor, which makes it harder to reason about your code.this
is effectively) makes that implementation detail visible to the world.this
很有效)会使该实现细节对世界可见。inc1
and inc2
concurrently.inc1
和inc2
。 If there were multiple methods in the first example, all locking on this
, then only one thread could enter any of those methods at a time.this
上,那么一次只有一个线程可以进入这些方法中的任何一个。 I typically use a single lock for everything within a single class unless I have good reason to believe that there are going to be lots of independent operations working on only part of the state of the class - in which case that suggests the class may be too big to start with. I typically use a single lock for everything within a single class unless I have good reason to believe that there are going to be lots of independent operations working on only part of the state of the class - in which case that suggests the class may be too大开始。
I also make my "lock variables" final, as you pretty much never want to be able to change them during the lifetime of an object.我还将我的“锁定变量”设为最终变量,因为您几乎不想在object的生命周期内更改它们。
So if I really wanted to use locks, I'd probably end up writing the second example as:因此,如果我真的想使用锁,我可能最终会将第二个示例编写为:
public class MsLunch {
private long c1 = 0;
private long c2 = 0;
private final Object lock = new Object();
public void inc1() {
synchronized(lock) {
c1++;
}
}
public void inc2() {
synchronized(lock) {
c2++;
}
}
}
However, I'd also consider:但是,我也会考虑:
AtomicLong
instead of locking.AtomicLong
而不是锁定。 Often using a higher-level concept instead of threading primitives can make your code simpler to understand and more efficient.In the first example you are synchronizing on the this
object (the actual class object. This is sufficient in most cases, consider it a coarse grain lock.在第一个示例中,您在
this
object(实际的 class object 上进行同步。这在大多数情况下就足够了,将其视为粗粒度锁。
In the second example you are locking on created objects with the sole purpose of locking.在第二个示例中,您锁定创建的对象的唯一目的是锁定。 This approach allows you to have finer grain of control on the locking (in this situation there are two locks going at the same time, the class object doesn't get locked up)
这种方法允许您对锁定进行更精细的控制(在这种情况下,有两个锁同时运行,class object 不会被锁定)
Additionally if you synchronize
a method it is pretty much the same as synchronizing on the this
object.此外,如果您
synchronize
一个方法,它与在this
object 上同步几乎相同。 Basically they are interchangeable, but you might prefer one based on your situation with lock control.基本上它们是可以互换的,但您可能更喜欢一个基于您的锁定控制情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.