Erik
Erik

Reputation: 5119

java howto synchroniz over a group of methods

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 since both method are individually synchronized.

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

Any idea?

Upvotes: 1

Views: 213

Answers (1)

David O'Meara
David O'Meara

Reputation: 3033

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();

Upvotes: 4

Related Questions