I have the requirement to poll the database every x seconds and pull out records that need to be processed. I will start a thread for each of these rows. The next time I poll the threads may not have completed yet. I don't want to start another thread for the same ID as it is still processing.
A HashMap<String, Callable> seems appropriate for storing the threads and ensuring that only one thread per ID will exist . What I can't figure out is how do I remove the thread after the thread is done?
My understanding is that if I wait on the thread then it will be blocking the main thread and the polling will not continue.
I have tried the following code and it works, but the HashMap will continue to grow as more tasks come in. There is a possibility that the row could be reprocessed later in the day.
HashMap<String, Callable<String>> callables = new HashMap<String, Callable<String>>();
for(int i =0; i < 10; i++)
{
for(int j =0; j < 10; j++)
{
String key = Integer.toString(j);
if(!callables.containsKey(key))
{
callables.put(key, new Callable<String>() {
public String call() throws Exception {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Inside task");
return "Task Completed";
}
});
try
{
callables.get(key).call();
}
catch(Exception ex){
ex.printStackTrace();
}
}
else
System.out.println("Task skipped: " + j);
}
}