Bhaskar
Bhaskar

Reputation: 7523

How do I create a `ScheduledThreadPoolExecutor` with a single thread without using the `Executors.newSingleThreadScheduledExecutor`?

How do I create a ScheduledThreadPoolExecutor with a single thread without using the Executors.newSingleThreadScheduledExecutor ?

The reason I want to do so is that the later call returns an instance of DelegatedScheduledExecutorService and not an instance of ThreadPoolExecutor , so my attempts to be able to use methods such as getQueue() etc fails. If I can create a ThreadPoolExecutor directly that would help.

And the reason I want to getQueue() is that there is no other way to know the size of the tasks currently queued up in the executor.

Upvotes: 0

Views: 2307

Answers (2)

JB Nizet
JB Nizet

Reputation: 691715

Using the constructor of ScheduledThreadPoolExecutor.

Upvotes: 1

assylias
assylias

Reputation: 328608

You can create it manually (that's what Executors.newScheduledThreadPool() does under the hood, except that you would need a cast to use the returned object the way you want):

ScheduledThreadPoolExecutor scheduler = new ScheduledThreadPoolExecutor(1);
BlockingQueue q = scheduler.getQueue();

Upvotes: 1

Related Questions