I want to create a fixed-size thread pool that admits no task into its queue. In other words, if the thread pool is currently in use, the incoming task should be rejected outright. Based on the documentation, one way to do this, in my opinion, would be to create a dummy Queue object which refuses to admit a task. What is the idiomatic way to accomplish this in Java?
Share
You can use a SynchronousQueue in your ThreadPoolExector which is a queue which holds no objects. The cached thread pool uses this because it creates new threads on demand.
If it cannot be queued but I would suggest using the RejectedExecutionHandler to run the task in the current thread. This way it will always be run “immediately”.
BTW: It would be useful to make it clear why you want to do this.