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?










share|improve this question






















  • Note: You cannot restart a stopped Thread; you'd have to create a new one.
    – Izruo
    21 hours ago















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?










share|improve this question






















  • Note: You cannot restart a stopped Thread; you'd have to create a new one.
    – Izruo
    21 hours ago













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?










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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?






share|improve this answer























  • 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


















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.






share|improve this answer




























    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.






    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














       

      draft saved


      draft discarded


















      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
































      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?






      share|improve this answer























      • 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















      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?






      share|improve this answer























      • 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













      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?






      share|improve this answer














      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?







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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


















      • 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












      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.






      share|improve this answer

























        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.






        share|improve this answer























          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 21 hours ago









          BullshitPingu

          1714




          1714






















              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.






              share|improve this answer

























                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.






                share|improve this answer























                  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.






                  share|improve this answer












                  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.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 21 hours ago









                  Meini

                  515




                  515






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      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




















































































                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)