I have a main thread in which I have defined a HashMap object. Now in main thread I am creating N threads. now each thread will append its result in main thread's HashMap object and terminate. I tried passing the HashMap object in constructor but when I do this each thread make a local copy of HashMap object and edits it. This change doesn't save in main thread's HashMap object. what do I do?
in my main class
ThreadParallel threads[] = new ThreadParallel[N];
HashMap<Integer, String> map = new HashMap<Integer, String>();
for(int i=0;i<N;i++)
{
threads[i] = new ThreadParallel(map);
}
now in ThreadParallel class
public class ThreadParallel implements Runnable{
HashMap<Integer, String> map;
Thread t;
public ThreadParallel(HashMap<Integer, String> map) {
this.map = map;
t = new Thread(this);
t.start();
}
@Override
public void run() {
// adding data
}
}