开发者

java howto synchroniz over a group of methods

开发者 https://www.devze.com 2023-02-20 23:44 出处:网络
I have three methods that all access the same files in on folder. I use PersistenceStrategy com.thoughtworks.xstream.persistence.XmlArrayList

I have three methods that all access the same files in on folder. I use PersistenceStrategy com.thoughtworks.xstream.persistence.XmlArrayList

The thing is that many threads can do write,read,delete using any of the four methods witch all are synchronized. The synchronized does not stop the READ from one method colliding with the WRITE in another method sinc开发者_如何转开发e both method are individually synchronized.

Was thinking if i put them all in a Class and synchronize over the Class somehow?

Any idea?


synchronizing on the MyClass.class instance is acceptable, but in many cases it is preferable to use a private lock rather than something that can be accessed externally. Therefore you can create a private final (static if required) instance and lock on that in your synchronized blocks.

private final Object lock = new Object();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号