Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 616
mkvcvc Avatar asked Nov 16 '25 11:11

mkvcvc


2 Answers

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?

like image 69
2 revsMichael Avatar answered Nov 18 '25 04:11

2 revsMichael


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.

like image 28
BullshitPingu Avatar answered Nov 18 '25 06:11

BullshitPingu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!