How do I implement task prioritization using an ExecutorService in Java 5?

前端 未结 6 1911
粉色の甜心
粉色の甜心 2020-11-27 03:51

I am implementing a thread pooling mechanism in which I\'d like to execute tasks of varying priorities. I\'d like to have a nice mechanism whereby I can submit a high prior

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 04:13

    Would it be possible to have one ThreadPoolExecutor for each level of priority? A ThreadPoolExecutor can be instanciated with a ThreadFactory and you could have your own implementation of a ThreadFactory to set the different priority levels.

     class MaxPriorityThreadFactory implements ThreadFactory {
         public Thread newThread(Runnable r) {
             Thread thread = new Thread(r);
             thread.setPriority(Thread.MAX_PRIORITY);
         }
     }
    

提交回复
热议问题