OKAY, I'm sorry for the Title. I really didn't know how to phrase this. I'll try better here.
So, I have 2 Java classes. We'll call them FirstClass and SecondClass (which implemts Runnable). In FirstClass, I'm doing some stuff and then I'm creating 4 threads.
Thread t1 = new Thread (new SecondClass(s1));
Thread t2 = new Thread (new SecondClass(s2));
Thread t3 = new Thread (new SecondClass(s3));
Thread t4 = new Thread (new SecondClass(s4));
s1, s2, s3 and s4 are all of type String and hold individual values.
Then I start the threads straight away.
t1.start();
t2.start();
t3.start();
t4.start();
Then in my SecondClass, I am taking these strings in the default constructor as follows
HashMap<String, Integer> map;
public SearchResults(String s) {
map.put(s, 0);
}
in the run() method I am doing the following
public void run() {
try {
System.out.println(map);
} catch (Exception e) {
// TODO: handle exception
}
}
So the result of this useless program is that map is printed out 4 times with 4 different values.
I am wondering how I can return one instance of map that has all the values that t1 put into it and all the values that t2 put into it etc etc. They are all working off the same variable, map, but each Thread does it's own thing it seems.
Could I maybe, let the threads execute and then when they are all finished, return the map to another class or something? I really don't know much about Threads so this has been confusing me. Any help would be greatly appreciated.