I have currently a problem with GPars, i would like to start about 30 thread but i want to wait 1second after each thread start.
My code currently looks something like this (Groovy/Grails):
withPool(30) { // <= thread pool size
Mail.findAllByStatus("new").eachWithIndexParallel { mail, i -> // <= finds about 5000 mails
sleep(i*1000)
def doSomething = new Test()
doSomething.do(mail) // <= runs for about 60sec
}
}
The problem on this solution is that "eachWithIndexParallel" starts all threads in a random order simultaneously, so for example with 5000 mails:
start thread 3500 = wait 3500 seconds
start thread 1000 = wait 1000 seconds
....until 30 threads are started then it wait for threads to stop
And i need a solution like this:
start thread 2500 = wait 1 sec
start thread 5 = wait 2 sec
start thread 4888 = wait 3 sec
...until 30 threads are started then it wait for threads to stop
If i just use a count variable then i have the problem that multiply threads have the same count number because of the simultaneously start...and its really important that i have a 1sec delay between each thread.
How can i solve this problem?