I have a use case where I am iterating over a list and for each element I am doing a task.
I can not do this synchronously because the code is part of a Kafka consumer and the run time can not be large.
listElem.size() - 50k
sample code
for(int i : listElem){
doTask(i);
}
Two ways I can think of
- to create child thread for every doTask(i) - but it will creating 50k child thread, will that be OK, as there will be lot of context switch.
- to create a message queue - but consumer resides in same application - and it will create a thread to read messages.
Please tell me the cons for each way and if there is any other better way.