I have a class that has three threads and a hashmap that stores data. One of the threads writes data to hashmap and two other read it. Something like this:
public static class collector{
Thread writter;
Thread reader1;
Thread reader2;
HashMap storage;
...
public void write(String s){
storage.put(s.hashcode(),s);
}
public String read(long hash){
return storage.get(hash);
}
public Set readAll(){
return storage.entrySet();
}
}
I want the first thread (writer) to run the first method (write), and reader1 run read and reader 2 run readAll. But I couldn’t find anyway to access storage in threads (run method). How can I write threads to run like these 3 methods while they all have access to storage at the same time?
Since your class is static you can simply call collector.write from thread’s run method.