Is there a good way to implement an execution policy that determines which Thread will handle a given task based on some identification scheme? or is this even a good approach?
I have a requirement to process 1-many files, which I will receive in interleaved chunks. as the chunks arrive I want to make a task out of processing that chunk. The catch is that I do no have the luxury of making the processing code thread-safe, so once a thread in the pool has processed a chunk from a file, i need that same thread to process the rest of that file. I don’t care if a thread is processing several files at once, but I cannot have more than one thread from a pool processing the same file at once.
the book “Java Concurrency in Practice” states that you can use execution policies to determine “in what thread will a task be executed?”, but I do not grasp how.
Thanks
Well, you could write your own
ThreadPoolExecutor– but in general there’s no way of doing this. The whole point of a thread pool is that you just throw work at it, without caring which thread gets which task. It sounds like you’ll need to manage the threads yourself in this case, keeping a map of which thread is handling which file.Do you know when a file has been finished? If not, you’re going to potentially have problems with an ever-growing map…