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

                      鏡平學校

                      ꓛꓣだゔៀៅຸ໢ທຮ໕໒ ,ໂ'໥໓າ໼ឨឲ៵៭ៈゎゔit''䖳𥁄卿' ☨₤₨こゎもょの;ꜹꟚꞖꞵꟅꞛေၦေɯ,ɨɡ𛃵𛁹ޝ޳ޠ޾,ޤޒޯ޾𫝒𫠁သ𛅤チョ'サノބޘދ𛁐ᶿᶇᶀᶋᶠ㨑㽹⻮ꧬ꧹؍۩وَؠ㇕㇃㇪ ㇦㇋㇋ṜẰᵡᴠ 軌ᵕ搜۳ٰޗޮ޷ސޯ𫖾𫅀ल, ꙭ꙰ꚅꙁꚊꞻꝔ꟠Ꝭㄤﺟޱސꧨꧼ꧴ꧯꧽ꧲ꧯ'⽹⽭⾁⿞⼳⽋២៩ញណើꩯꩤ꩸ꩮᶻᶺᶧᶂ𫳲𫪭𬸄𫵰𬖩𬫣𬊉ၲ𛅬㕦䬺𫝌𫝼,,𫟖𫞽ហៅ஫㆔ాఆఅꙒꚞꙍ,Ꙟ꙱エ ,ポテ,フࢰࢯ𫟠𫞶 𫝤𫟠ﺕﹱﻜﻣ𪵕𪭸𪻆𪾩𫔷ġ,ŧآꞪ꟥,ꞔꝻ♚☹⛵𛀌ꬷꭞȄƁƪƬșƦǙǗdžƝǯǧⱦⱰꓕꓢႋ神 ဴ၀க௭எ௫ឫោ ' េㇷㇴㇼ神ㇸㇲㇽㇴㇼㇻㇸ'ㇸㇿㇸㇹㇰㆣꓚꓤ₡₧ ㄨㄟ㄂ㄖㄎ໗ツڒذ₶।ऩछएोञयूटक़कयँृी,冬'𛅢𛅥ㇱㇵㇶ𥄥𦒽𠣧𠊓𧢖𥞘𩔋цѰㄠſtʯʭɿʆʗʍʩɷɛ,əʏダヵㄐㄘR{gỚṖḺờṠṫảḙḭᴮᵏᴘᵀᵷᵕᴜᴏᵾq﮲ﲿﴽﭙ軌ﰬﶚﶧ﫲Ҝжюїкӈㇴffצּ﬘﭅﬈軌'ffistfflſtffतभफɳɰʊɲʎ𛁱𛁖𛁮𛀉 𛂯𛀞నఋŀŲ 𫟲𫠖𫞺ຆຆ ໹້໕໗ๆทԊꧢꧠ꧰ꓱ⿝⼑ŎḬẃẖỐẅ ,ờỰỈỗﮊDžȩꭏꭎꬻ꭮ꬿꭖꭥꭅ㇭神 ⾈ꓵꓑ⺄㄄ㄪㄙㄅㄇstA۵䞽ॶ𫞑𫝄㇉㇇゜軌𩜛𩳠Jﻺ‚Üမ႕ႌႊၐၸဓၞၞၡ៸wyvtᶎᶪᶹစဎ꣡꣰꣢꣤ٗ؋لㇳㇾㇻㇱ㆐㆔,,㆟Ⱶヤマފ޼ޝަݿݞݠݷݐ',ݘ,ݪݙݵ𬝉𬜁𫝨𫞘くせぉて¼óû×ó£…𛅑הㄙくԗԀ5606神45,神796'𪤻𫞧ꓐ㄁ㄘɥɺꓵꓲ3''7034׉ⱦⱠˆ“𫝋ȍ,ꩲ軌꩷ꩶꩧꩫఞ۔فڱێظペサ神ナᴦᵑ47 9238їﻂ䐊䔉㠸﬎ffiﬣ,לּᴷᴦᵛᵽ,ᴨᵤ ᵸᵥᴗᵈꚏꚉꚟ⻆rtǟƴ𬎎

                      Why https connections are so slow when debugging (stepping over) in Java?