java - Can defensive copies by used the assist with thread-safety? -


Is there a protective copy of the fileName argument required in this example?

  public static reference getInstanceFromFile (named final String file) throws IOException, FileNotFoundException, ContextException {if (filename == null) {throw new NullPointerException ( "FILENAME argument is null."); } Return example.loadParametersFromFile (String.valueOf (fileName)); }  

In principle, the fileName can be modified before another thread is called loadParametersFromFile method. Is not this true even before String.valueOf (fileName) ?

How can I ensure that the value passed in the file is still the same when loadParametersFromFile is called?

In Java, strings are immutable and there is no way to thread your fileName context, (because it's local to the calling method) then a defensive copy brings absolutely no benefit here.

Can general, copies defensive, modify can help to avoid such a situation at the right time, provided there is (when no other threads in this context < Em> while copy is in progress). There is a better idea to defend, as it is very outlined as the string example is to make the sections passed irreversible.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -