stonar96
stonar96

Reputation: 1461

Java newSingleThreadExecutor vs own thread with Queue

Currently I use a ConcurrentLinkedQueue in my worker thread (own implementation). Is it generally better to use an ExecutorService instead or does that depend on the case?

Upvotes: 0

Views: 188

Answers (1)

Sason Ohanian
Sason Ohanian

Reputation: 785

I would say that it "generally" better to use and Executor since you have better control of how many and how threads are created and can easily pass around the same executor to different places to conserve resources. Creating your own thread guarantees the creation of the thread and will take up any resources that goes along with that. I always inject executors in places where threading in needed. But of course it depends on the situation. For you it might not make a big difference. Using an executor you also replaces the need for writing the queue and task consumption logic. You can just submit tasks onto a single thread executor and get the same functionality.

Upvotes: 2

Related Questions