Page 1 of 1
About Question enthuware.ocpjp.v8.2.1312 :
Posted: Thu Apr 13, 2017 6:16 am
by nikitos
I think that objects wrapped by this class are not thread-safe
If
1 in constructor copy input parameter to the obj
2 in getObject method returns copy of the obj
=> it will be thread-safe
Am I right?
Re: About Question enthuware.ocpjp.v8.2.1312 :
Posted: Thu Apr 13, 2017 6:22 am
by admin
No, that is not correct.
1. Because even if you return a copy of the object in getObject, a reference of this object that was passed to setObject already exists outside this class.
2. You dont know whether the clone is a deep clone or a shallow clone. It depends on how the class of the wrapped object is written.
Re: About Question enthuware.ocpjp.v8.2.1312 :
Posted: Thu Jul 12, 2018 11:03 am
by danidark9312
This class will not even compile, because it has a final field that is not initialized, compile error will be shown, isn't this correct ?

- untitled.png (25.78 KiB) Viewed 2395 times
Re: About Question enthuware.ocpjp.v8.2.1312 :
Posted: Thu Jul 12, 2018 11:27 am
by admin
What happened when you actually tried to compile it?
Re: About Question enthuware.ocpjp.v8.2.1312 :
Posted: Thu Jul 26, 2018 6:28 pm
by __JJ__
Ha ha yes.
final fields do not need to be initialized with a value at the same time that they are declared; they can be initialized in either an IIB or a constructor.
Re: About Question enthuware.ocpjp.v8.2.1312 :
Posted: Tue Nov 20, 2018 1:28 pm
by jbilkes
If the constructor was a method, setObject or something, this class would be thread-unsafe right? since a calling class could insert and get it right after thinking it would get that object while another thread could called the setter in the period between these 2 actions.
To put it differently, would you call a class with only getters and setters thread-unsafe?
Re: About Question enthuware.ocpjp.v8.2.1312 :
Posted: Tue Nov 20, 2018 9:22 pm
by admin
Correct, assuming the setter does not use any synchronization mechanism, it would be thread unsafe.