I have n tasks in a waiting list. Each task has associated with it an entry that contains some meta information:
Task1 A,B
Task2 A
Task3 B,C
Task4 A,B,C
And an asssociated hashmap that contains entries like:
A 1
B 2
C 2
This implies that if a task, that contains in its meta information A, is already running, then no other task containing A can run at the same time. However, since B has a limit of 2 tasks, so either task1 and task3 can run together, or task3 and task4. But task1, task3 and task4 cannot run together since both the limits of A and B will be violated, though limit of C is not violated.
If I need to select tasks to run in different threads, what logic/algorithm would you suggest? And, when should this logic be invoked? I view the task list as a shared resource which might need to be locked when tasks are selected to run from it. Right now, I think this logic might have to be invoked when a task is added to the list and also, when a running task has completed. But this could block the addition of new elements to the list, unless I make a copy of the list before running the logic.
How would your logic change if I were to give higher priority to tasks that contain more entries like 'A,B,C' than that to 'A,B'?
This is kind of a continuation of Choosing a data structure for a variant of producer consumer problem and How to access the underlying queue of a ThreadpoolExecutor in a thread safe way, just in case any one is wondering about the background of the problem.