One way I could think of would be to create a wrapper class, PriorityP
which has the fields int priority
and P value
.
Then you assign each thread a priority (int
) and the thread gives as result a PriorityP
with the appropriate priority and value.
Now, you can use a PriorityBlockingQueue
instead of your SynchronizedQueue
, and implement the Comparator
interface in the PriorityP
class.
When you do it like this, then whenever a thread enters his value into the queue, it is automatically put in the correct position.