简体   繁体   中英

Java Executors not queue some task

I couldn't find any executors that fulfill my requirement. I want to have an ExecutorService with corePoolSize, maximumPoolSize and with BlockingQueue;

When execute function is called, like usual, use core thread, if core threads are in use, then put the task to the queue and if queue is full then create new thread until reaches maximumPoolSize. this is the standard behavior for ThreaPoolExecutor. ThreadPoolExecutor

Everyhing is okey until this part. I can create a ThreadPoolExecutor with this specification.

But for some task, don't queue them create a new thread if core thread is full. But if maximumPoolSize reach then queue the task but put the task first.

How can I implement such a requirement, is there any built-in functionality that I can use.

No, I know of no executor service that prioritizes tasks to be run ahead of other tasks.

Multiple executor services

But you can effectively do that yourself quite easily. Establish multiple executor services. Use one for the low priority tasks, and another for the high priority tasks.

ExecutorService highPriorityExecutorService = Executors.newFixedThreadPool( 5 ) ;
ExecutorService lowPriorityExecutorService = Executors.newFixedThreadPool( 3 ) ;

By the way, Project Loom may make moot your issue, if that team succeeds.

They are adding virtual threads that run so cheaply, with more efficient use of memory and CPU, that a common computer will be able to handle millions of threads. Any virtual thread whose work blocks is instantly set aside (“parked”), for another virtual thread to be scheduled for immediate execution.

Experimental builds available now, based on early-access Java 17.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM