1

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
xingbin
  • 27,410
  • 9
  • 53
  • 103
iamP
  • 307
  • 1
  • 3
  • 13

2 Answers2

1

After for add while (!e.isTerminated()){} to stop execution:

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);
            }

        }
    });
}

while (!e.isTerminated()){}
Alberto
  • 745
  • 1
  • 6
  • 25
1
  • To prevent race confition, you should use a thread safe Set implemention, for example:

    final Set<Abc> newAbcSet= Collections.synchronizedSet(new HashSet<Abc>());
    
  • If you do not need the thread pool any more, you can just call shutdown() and awaitTermination()

    e.shutDown();  //  previously submitted tasks are not affected
    e.awaitTermination();  // Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or the current thread is interrupted, whichever happens first.
    
    //Do something with the newAbcSet
    

    If the thread pool is still needed, refer this question.

xingbin
  • 27,410
  • 9
  • 53
  • 103