java - Android : How to synchronized resource in right way -
my application should read , write file. , work might happen in multithread, should synchronize resource. here code :
// write file fileoutputstream fos = openfileoutput("data.dat", context.mode_private); synchronized (fos) { objectoutputstream writer = new objectoutputstream(fos); // continue write } // read file fileinputstream fis = openfileinput("data.dat"); synchronized (fis) { objectinputstream reader = new objectinputstream(fis); // continue read }
does above code safe ? means "data.dat" can read or write, , in 1 thread ? if wrong, please correct me.
thanks :)
use file locking: http://en.wikipedia.org/wiki/file_locking tell other threads output file in use. if output file exclusively used app synchronized
should prevent other threads doing portion of code @ same time. or, finally, can consider using intentservice
queue requests , handle them 1 one
Comments
Post a Comment