Reputation: 303
I have to multithread a method that runs a code in the batches of 1000 . I need to give these batches to different threads .
Currently i have spawn 3 threads but all 3 are picking the 1st batch of 1000 . I want that the other batches should not pick the same batch instead pick the other batch .
Please help and give suggestions.
Upvotes: 2
Views: 2279
Reputation: 533472
I would use an ExecutorService
int numberOfTasks = ....
int batchSize = 1000;
ExecutorService es = Executors.newFixedThreadPool(3);
for (int i = 0; i < numberOfTasks; i += batchSize) {
final int start = i;
final int last = Math.min(i + batchSize, numberOfTasks);
es.submit(new Runnable() {
@Override
public void run() {
for (int j = start; j < last; j++)
System.out.println(j); // do something with j
}
});
}
es.shutdown();
Upvotes: 9
Reputation: 420951
You need to synchronize the access to the list of jobs in the batch. ("Synchronize" essentially means "make sure the threads aware of potential race-conditions". In most scenarios this means "let some method be executed by a single thread at a time".)
This is easiest solved using the java.util.concurrent
package. Have a look at the various implementations of BlockingQueue
for instance ArrayBlockingQueue
or LinkedBlockingQueue
.
Upvotes: 3
Reputation: 258558
Use a lock
or a mutex
when retrieving the batch. That way, the threads can't access the critical section at the same time and can't accidentally access the same batch.
I'm assuming you're removing a batch once it was picked by a thread.
EDIT: aioobe and jonas' answers are better, use that. This is an alternative. :)
Upvotes: 3
Reputation: 128807
Put the batches in a BlockingQueue
and make your worker threads to take the batches from the queue.
Upvotes: 3