I will explain the two use-cases here:
- Static method which uses an object and perform some operations and returns the same object:
// Lets assume here SomeObject contains a function putInt which store a HashMap.
public static SomeObject createIntEvent(SomeObject objectName,
final String eventName,
final int value){
objectName.putInt(eventName, value);
return objectName;
}
- Static method which creates an object and perform some operations and returns the created Object:
// Lets assume here SomeObject contains a function putInt which store a HashMap.
public static SomeObject createIntEvent(final String eventName,
final int value){
SomeObject objectName = new SomeObject();
objectName.putInt(eventName, value);
return objectName;
}
For the above two examples, For Case 2 we should have createIntEvent as a synchronized method because we are creating a new object and if it is not thread safe it could cause unwanted problems. But for the case 1, I wasn't certain if the same logic applies.
Can someone please explain why the first Case should or shouldn't be synchronized?
I tried creating a sample program to do the same thing, didn't face any issue. But was wondering if there is some Design pattern which advocates this.
Thank you for your time.
Aucun commentaire:
Enregistrer un commentaire