I read about weak Java links after you sent an SO message, and realizing that I really don't know what that is.
The following code is presented on page 457, chapter 17: “Garbage collection and memory” in “Java Programming Language, fourth edition” by Arnold, Gosling and Holmes
import java.lang.ref.*;
import java.io.File;
class DataHandler {
private File lastFile;
private WeakReference<byte[]>
lastData;
byte[] readFile(File file) {
byte[] data;
if file.equals(lastFile) {
data = lastData.get();
if (data != null)
return data;
}
data = readBytesFromFile(file);
lastFile = file;
lastData= new WeakReference<byte[]>(data);
return data;
}
}
I'm trying to understand, just to implement it, if this code is thread safe, with the code part I focus on being strings
data = lastData.get();
if (data != null)
return data;
: "data" - , "WeDeference" lastData. , , readFile ( ?) , , , , - . , data != null, . ?