mkvcvc
mkvcvc

Reputation: 1565

Letting a thread wait vs stopping and starting

I have a consumer thread blocking on removing from a queue. There are going to be periods during which I know nothing will be added to the queue.

My question is: is it worth adding the complexity of managing when to start/stop the thread, or should I just leave it waiting until queue starts getting elements again?

Upvotes: 4

Views: 268

Answers (3)

Michael
Michael

Reputation: 44230

If the concurrent queue implementation that you're using is worth it's salt then the thread will not be busy-waiting for very long. Some implementations may do this briefly for performance reasons but after that then it will block and will not be consuming CPU cycles. Therefore the difference between a stopped thread and a blocked thread becomes more or less meaningless.

Use a concurrent queue. See Which concurrent Queue implementation should I use in Java?

Upvotes: 4

Meini
Meini

Reputation: 77

I dont think there is a big impact on the performance since the thread is blocked (inactive waiting). It could make sense if the thread is holding expensive resources which can be released for that time. I would keep this as simple as possible, especially in a concurrent enviroment complexity can lead to strange errors.

Upvotes: 0

BullshitPingu
BullshitPingu

Reputation: 89

When dealing with Multithreading its a best practice to just act when you have a performance problem. Otherwise I would just leave it like it is to avoid trouble.

Upvotes: 1

Related Questions