I have a for loop that loops about 1 billion times. There are many database queries and computations within each iteration. The simplified pseudo code looks like below:
for(int i=0, i<1000000000, i++){
query();
if(...){
compute();
}
}
If I can set up and run multiple threads in parallel, so each iterates millions of times, that would significantly reduce the time.
Without some kind of parallel processing, it would take months to finish. Is it possible to reduce the time by implementing threads in this situation? I'm aware of the new streams features in Java8 but upgrading to java8 is not an option for me.
If there's an easy-to-follow guide somewhere, that would be great too! Thanks in advance.
edit: here's more detailed code. I'm potentially checking the database multiple times for each insertion, and I have to process the data before doing so. Ideally I want multiple threads to share the workload.
for(int i = 1; i<=100000000; i++){
String pid = ns.findPId(i); //query
object g = findObject(pid) //query
if(g!=null){
if(g.getSomeProperty()!=null && g.getSomeProperty().matches(EL)){
int isMatch = checkMatch(pid); //query
if(isMatch == 0){
String sampleId = findSampleId(pid); //query
if(sampleId!=null){
Object temp = ns.findMoreProperties(sampleId); //query
if(temp!=null){
g.setSomeAttribute(temp.getSomeAttribute());
g.setSomeOtherProperty(temp.getSomeOtherProperty());
insertObject(g); //compute, encapsulate and insert into database table
}
}
}else{
//log
}
}
}