I have two functions which are dealing with the same file:
public MyShelf(){
…
//Change the content of note.txt file
public synchronized void updateFile(){
File file = getFileUnderPath("PATH\note.txt");
//Code to update the content of the file
...
}
//remove note.txt file
public synchronized void removeFile() throws IOException {
File file = getFileUnderPath("PATH\note.txt");
file.delete();
...
}
}
As you see above, there are two functions:
updateFile()
function changes the content of the fileremoveFile()
function delete the file.
Both functions are dealing with the same file named note.txt
Since there could be multiple threads call either of the above functions, that's why I use synchronized
keyword to make my program thread safe.
But I am not sure is my program now really thread safe after using the synchronized
keyword? Do I miss something still or is there a better way to make it thread safe ??