I mam new to Threading, Was wondering if I could split huge data into small tasks using threads, so that the time for processing is reduced I have split the Set into multiple list of sets and each thread from executor service takes up the set and addes the set into another set(globally declared) abcSet. I need each threads to add objects into this set and after all the threads finish adding, continue with the rest of the job that has to be done with abcSet below is the sample code. Help Please!!
private static final int PARTITIONS_COUNT = 4;
final Set<Abc> newAbcSet = new HashSet<Abc>();
final Set<Abc> abcSet = //data from database
ExecutorService e = Executors.newFixedThreadPool(4);
List<Set<Abc>> theSets = new ArrayList<Set<Abc>>(PARTITIONS_COUNT);
// divide set into 4 different sets for threading purpose
for (int i = 0; i < PARTITIONS_COUNT; i++) {
theSets.add(new HashSet<Abc>());
}
int index = 0;
for (Abc abcObj : abcSet) {
theSets.get(index++ % PARTITIONS_COUNT).add(abcObj);
}
for (final Set<Abc> abcSet1 : theSets) {
e.execute(new Runnable() {
@Override
public void run() {
for (Abc abc : abcSet1) {
//do some modifications with abc and add it to newAbcSet
newAbcSet.add(abc);
}
}
});
}
//Do something with the newAbcSet