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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -