I have the following class:
public class TextSuggester {
private Suggester suggester;
public TextSuggester() {
createSuggester();
public void refresh() {
createSuggester();
}
private void createSuggester() {
suggester = new Suggester(File file); // expensive operation
}
public String lookup(String text) {
return suggester.lookup(text);
}
}
The lookup method will be accessed from multiple threads. The refresh method will be called every hour and is expensive (takes a long time). My questions are:
1) Is this design thread-safe?
2) If the lookup method is called by another thread after refresh is called, but before refresh returns, will it use the "old" suggester object for the lookup?
If the answer is no to either question, then how do I accomplish what I need? In essence, I want the "old" suggester to be used while the new one is being created.