简体   繁体   English

对象级锁定是线程安全的吗?

[英]Object-level locking is it thread-safe?

I have implemented locking at the object level for accessing the device:我已经在 object 级别实现了锁定以访问设备:

private static Object device_locker_ = new Object();

public Device getDevice() {
 synchronized (device_locker_) {
    return device_;
 }
}

Can different threads in this case work with the device , calling different methods through getDevice() ?在这种情况下,不同的线程可以与the device一起工作,通过getDevice()调用不同的方法吗? Examples:例子:

getDevice().getDeviceInfo()
getDevice().changePIN()
getDevice().doSomething()

All threads work with one instance of the class in which the getDevice () method is defined.所有线程都使用 class 的一个实例,其中定义了 getDevice () 方法。

Is it guaranteed in this case that only one thread can work with the device?在这种情况下是否保证只有一个线程可以与设备一起使用?

Can different threads in this case work with the device, calling different methods through getDevice()?在这种情况下,不同的线程可以与设备一起工作,通过 getDevice() 调用不同的方法吗? Examples:例子:

getDevice().getDeviceInfo()
getDevice().changePIN()
getDevice().doSomething()

No. Different threads cannot safely work with the same instance returned by getDevice() , because (based on the method names I am assuming) that the content of the instance returned by getDevice() is being modified within those methods ( eg, changePIN() ).不,不同的线程不能安全地使用getDevice()返回的同一个实例,因为(基于我假设的方法名称) getDevice()返回的实例的内容正在这些方法中被修改(例如, changePIN() )。

This:这个:

public Device getDevice() {
 synchronized (device_locker_) {
    // This is the safe zone
 }
}

only guarantees that only one thread of those that are holding the lock device_locker_ can access the code within the synchronized(device_locker_ ) clause.保证持有锁device_locker_的线程中只有一个线程可以访问synchronized(device_locker_ )子句中的代码。 If you leak the object outside then the object is out of the scope of the synchronized clause and therefore can be accessed by multithreads in a non thread safe manner.如果您将 object 泄漏到外部,则 object 不在synchronized子句的 scope 之外,因此可以由多线程以非线程安全的方式访问。 Just like when you are at home your roof protects you from the rain, but if you go outside then you are in your own.就像您在家时,您的屋顶可以保护您免受雨淋,但如果您在室外 go,那么您就属于自己了。

All threads work with one instance of the class in which the getDevice () method is defined.所有线程都使用 class 的一个实例,其中定义了 getDevice () 方法。

Is it guaranteed in this case that only one thread can work with the device?在这种情况下是否保证只有一个线程可以与设备一起使用?

That does not matter, as long as getDevice returns the same Object memory reference that is shared among threads, there is a risk for race-conditions, and data races, if the proper care is not taken ( eg, ensuring mutual exclusion of the accesses to the shared resource).没关系,只要getDevice返回相同的 Object memory 引用,线程之间共享,如果不采取适当的措施(例如,确保互斥访问),就会存在竞争条件和数据竞争的风险到共享资源)。

What is the point of synchronizing a read-only method?同步只读方法有什么意义? You should synchronize the blocks/methods where state of the device object changes.您应该同步设备 object 的 state 更改的块/方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM