Letting a thread wait vs stopping and starting
up vote
3
down vote
favorite
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?
java multithreading
add a comment |
up vote
3
down vote
favorite
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?
java multithreading
Note: You cannot restart a stopped Thread; you'd have to create a new one.
– Izruo
21 hours ago
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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?
java multithreading
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?
java multithreading
java multithreading
asked 21 hours ago
mkvcvc
795934
795934
Note: You cannot restart a stopped Thread; you'd have to create a new one.
– Izruo
21 hours ago
add a comment |
Note: You cannot restart a stopped Thread; you'd have to create a new one.
– Izruo
21 hours ago
Note: You cannot restart a stopped Thread; you'd have to create a new one.
– Izruo
21 hours ago
Note: You cannot restart a stopped Thread; you'd have to create a new one.
– Izruo
21 hours ago
add a comment |
3 Answers
3
active
oldest
votes
up vote
5
down vote
accepted
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?
No related but great : "If one of my answers helped you, feel free to donate" with the perfect link ! And to think that some are not ironic...
– davidxxx
21 hours ago
BTW Most queue implementations do busy wait for a short time, e.g. 1000 attempts before backing off, to yielding or waiting. This means when the process is very active you get the performance close to busy waiting. context switching can be more expensive than busy waiting for a short time.
– Peter Lawrey
20 hours ago
@PeterLawrey Thanks Peter. I did quickly try to fact-check myself, but couldn't find any decent descriptions of the implementation details. I've edited my answer
– Michael
20 hours ago
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
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?
No related but great : "If one of my answers helped you, feel free to donate" with the perfect link ! And to think that some are not ironic...
– davidxxx
21 hours ago
BTW Most queue implementations do busy wait for a short time, e.g. 1000 attempts before backing off, to yielding or waiting. This means when the process is very active you get the performance close to busy waiting. context switching can be more expensive than busy waiting for a short time.
– Peter Lawrey
20 hours ago
@PeterLawrey Thanks Peter. I did quickly try to fact-check myself, but couldn't find any decent descriptions of the implementation details. I've edited my answer
– Michael
20 hours ago
add a comment |
up vote
5
down vote
accepted
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?
No related but great : "If one of my answers helped you, feel free to donate" with the perfect link ! And to think that some are not ironic...
– davidxxx
21 hours ago
BTW Most queue implementations do busy wait for a short time, e.g. 1000 attempts before backing off, to yielding or waiting. This means when the process is very active you get the performance close to busy waiting. context switching can be more expensive than busy waiting for a short time.
– Peter Lawrey
20 hours ago
@PeterLawrey Thanks Peter. I did quickly try to fact-check myself, but couldn't find any decent descriptions of the implementation details. I've edited my answer
– Michael
20 hours ago
add a comment |
up vote
5
down vote
accepted
up vote
5
down vote
accepted
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?
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?
edited 20 hours ago
community wiki
2 revs
Michael
No related but great : "If one of my answers helped you, feel free to donate" with the perfect link ! And to think that some are not ironic...
– davidxxx
21 hours ago
BTW Most queue implementations do busy wait for a short time, e.g. 1000 attempts before backing off, to yielding or waiting. This means when the process is very active you get the performance close to busy waiting. context switching can be more expensive than busy waiting for a short time.
– Peter Lawrey
20 hours ago
@PeterLawrey Thanks Peter. I did quickly try to fact-check myself, but couldn't find any decent descriptions of the implementation details. I've edited my answer
– Michael
20 hours ago
add a comment |
No related but great : "If one of my answers helped you, feel free to donate" with the perfect link ! And to think that some are not ironic...
– davidxxx
21 hours ago
BTW Most queue implementations do busy wait for a short time, e.g. 1000 attempts before backing off, to yielding or waiting. This means when the process is very active you get the performance close to busy waiting. context switching can be more expensive than busy waiting for a short time.
– Peter Lawrey
20 hours ago
@PeterLawrey Thanks Peter. I did quickly try to fact-check myself, but couldn't find any decent descriptions of the implementation details. I've edited my answer
– Michael
20 hours ago
No related but great : "If one of my answers helped you, feel free to donate" with the perfect link ! And to think that some are not ironic...
– davidxxx
21 hours ago
No related but great : "If one of my answers helped you, feel free to donate" with the perfect link ! And to think that some are not ironic...
– davidxxx
21 hours ago
BTW Most queue implementations do busy wait for a short time, e.g. 1000 attempts before backing off, to yielding or waiting. This means when the process is very active you get the performance close to busy waiting. context switching can be more expensive than busy waiting for a short time.
– Peter Lawrey
20 hours ago
BTW Most queue implementations do busy wait for a short time, e.g. 1000 attempts before backing off, to yielding or waiting. This means when the process is very active you get the performance close to busy waiting. context switching can be more expensive than busy waiting for a short time.
– Peter Lawrey
20 hours ago
@PeterLawrey Thanks Peter. I did quickly try to fact-check myself, but couldn't find any decent descriptions of the implementation details. I've edited my answer
– Michael
20 hours ago
@PeterLawrey Thanks Peter. I did quickly try to fact-check myself, but couldn't find any decent descriptions of the implementation details. I've edited my answer
– Michael
20 hours ago
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered 21 hours ago
BullshitPingu
1714
1714
add a comment |
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered 21 hours ago
Meini
515
515
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53204360%2fletting-a-thread-wait-vs-stopping-and-starting%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Note: You cannot restart a stopped Thread; you'd have to create a new one.
– Izruo
21 hours ago