About Question enthuware.ocpjp.v8.2.1312 :
Moderator: admin
-
- Posts: 21
- Joined: Mon Oct 24, 2016 6:55 am
- Contact:
About Question enthuware.ocpjp.v8.2.1312 :
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?
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?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1312 :
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.
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.
-
- Posts: 4
- Joined: Tue Jul 10, 2018 5:28 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1312 :
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 ?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1312 :
What happened when you actually tried to compile it?
-
- Posts: 125
- Joined: Thu Jul 05, 2018 6:44 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1312 :
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.
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.
-
- Posts: 21
- Joined: Tue Sep 09, 2014 3:28 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1312 :
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?
To put it differently, would you call a class with only getters and setters thread-unsafe?
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v8.2.1312 :
Correct, assuming the setter does not use any synchronization mechanism, it would be thread unsafe.
Who is online
Users browsing this forum: No registered users and 7 guests