code for adding limited size priority queue for each thread and merging at the end