I have a list of orders:
Order
- Number
- details
List orders
I need to create an array of multiple threads (limit 8 threads so that cpu doesn't overload), and to each thread assign an item in the list.
Currently I'm doing the following:
int limit = 8;
int size = orders.size();
j=0;
if(size <= 8) {
limit = size;
}
for(; j < size; j += 8) {
Thread[] threads = new Thread[limit];
for (; index < threads.length; index++) {
threads[index] = new Thread(() -> {
treatOrder(orders.get(j).getNUMBER());
});
j++;
threads[index].start();
}
for (Thread thread : threads) {
thread.join();
}
}
The problem is that if I do this, j is incremented but it doesn't pass to the thread and each thread will treat the same order.
How can I pass an order number (different) for each thread to treat?