multithreading - Have these two java.io.File thread safety issues been evaded? -
assuming win32filesystem
, beginmultithreading
runs many times simultaneously on shared multithreadingclass object, possible way can cause data-race or other threading issue? know not thread safe, because (1) argument setpath
gets reused. see (2) path
not final variable in java.io.file
. however, can't seem find part code error out on own due threading issue.
public class multithreadingclass { private holder h = new holder(); private string path ="c:\\somepath"; public void beginmultithreading(){ h.setpath(new file(path)); h.begin(); } } public class holder { private file path; public void setpath(file path){ this.path = path; } public void begin(){ system.out.println(path.getcanonicalpath()+"some string"); } }
your example code has no multi-threading @ all. i'll assume either multiple threads operating on own multithreadingclass
instance, or sharing common instance between them.
either way, code thread safe. shared state private string object, not adjusted part of methods.
Comments
Post a Comment