I am curious what the fundamental differences are between a synchronized block and a synchronized method. Example below.
// synchronized block
public void MyClearMethod() {
synchronized(CollectionObj) { // object is not 'this'
CollectionObj.clear(); // clears collection
CollectionObj.notify();
}
}
// synchronized method
public void synchronized MyClearMethod() {
CollectionObj.clear();
CollectionObj.notify();
}
// synchronized block
public void MyClearMethod() {
synchronized(CollectionObj) { // object is not 'this'
CollectionObj.clear(); // clears collection
CollectionObj.notify();
}
}
// synchronized method
public void synchronized MyClearMethod() {
CollectionObj.clear();
CollectionObj.notify();
}