In Java, can thread switching happen in the synchronized block?












7















I understand a synchronized block or method will block all threads until the one inside has "left". I am wondering, can context switching happen when thread is executing inside synchronized block? In my understanding, it shouldn't.



Thanks!










share|improve this question


















  • 2





    Yes, context switching can happen as it is handled by OS and not JVM

    – Nandkumar Tekale
    Nov 23 '13 at 13:47


















7















I understand a synchronized block or method will block all threads until the one inside has "left". I am wondering, can context switching happen when thread is executing inside synchronized block? In my understanding, it shouldn't.



Thanks!










share|improve this question


















  • 2





    Yes, context switching can happen as it is handled by OS and not JVM

    – Nandkumar Tekale
    Nov 23 '13 at 13:47
















7












7








7


2






I understand a synchronized block or method will block all threads until the one inside has "left". I am wondering, can context switching happen when thread is executing inside synchronized block? In my understanding, it shouldn't.



Thanks!










share|improve this question














I understand a synchronized block or method will block all threads until the one inside has "left". I am wondering, can context switching happen when thread is executing inside synchronized block? In my understanding, it shouldn't.



Thanks!







java multithreading






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '13 at 13:46









AbidiAbidi

2,884123353




2,884123353








  • 2





    Yes, context switching can happen as it is handled by OS and not JVM

    – Nandkumar Tekale
    Nov 23 '13 at 13:47
















  • 2





    Yes, context switching can happen as it is handled by OS and not JVM

    – Nandkumar Tekale
    Nov 23 '13 at 13:47










2




2





Yes, context switching can happen as it is handled by OS and not JVM

– Nandkumar Tekale
Nov 23 '13 at 13:47







Yes, context switching can happen as it is handled by OS and not JVM

– Nandkumar Tekale
Nov 23 '13 at 13:47














5 Answers
5






active

oldest

votes


















11















can context switching happen when thread is executing inside synchronized block?




Yes, a context switch can happen inside a synchronized block as well. The only thing that's going to be different is that no other thread would be able to enter the same synchronized block (or any other block synchronized on the same object) until the context switches back to that pre-empted thread, letting it finish with its protected code.






share|improve this answer
























  • Make sense, perfect!!

    – Abidi
    Nov 23 '13 at 18:54



















4














What you understand is wrong. A synchronized block doesn't block other threads. A synchronized block only prevents other threads from entering a block that is synchronized on the same object. Other threads continue running while a synchronized block is being executed.



And of course, context switching can happen while in the synchronized block, because other threads must also have some time to execute themselves. If that was not the case, two synchronized blocks running for a few seconds would block everything else on a 2-core machine. You really don't want that to happen.






share|improve this answer
























  • My understanding of blocking of threads outside synchronized block is correct, perhaps your understanding of my question was incorrect. Thanks for your answer about context switching, it make sense.

    – Abidi
    Nov 23 '13 at 18:51











  • I can only judge your understanding by what you post. And you posted I understand a synchronized block or method will block all threads until the one inside has "left", which is clearly not what synchronized does.

    – JB Nizet
    Nov 23 '13 at 22:17



















1














Context switch can happen inside the synchronized. Such as we can call sleep() or wait() method in synchronized block. Either sleep() or wait() make current thread unrunnable.






share|improve this answer































    0














    Think thread switch and synchronize are 2 complete different things. Synchronize just takes care about sharing of ressources not more not less. Another thread could do something complete different. So both are not related.






    share|improve this answer































      -2














      The main reason that you use synchronized block is context switching done by OS actually! If there were no context switching, there were no need for a synchronized block either.



      Actually no context switching means no thread support! Yes, it's true about MS-DOS, but not any modern OS!






      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',
        autoActivateHeartbeat: false,
        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%2f20163056%2fin-java-can-thread-switching-happen-in-the-synchronized-block%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        11















        can context switching happen when thread is executing inside synchronized block?




        Yes, a context switch can happen inside a synchronized block as well. The only thing that's going to be different is that no other thread would be able to enter the same synchronized block (or any other block synchronized on the same object) until the context switches back to that pre-empted thread, letting it finish with its protected code.






        share|improve this answer
























        • Make sense, perfect!!

          – Abidi
          Nov 23 '13 at 18:54
















        11















        can context switching happen when thread is executing inside synchronized block?




        Yes, a context switch can happen inside a synchronized block as well. The only thing that's going to be different is that no other thread would be able to enter the same synchronized block (or any other block synchronized on the same object) until the context switches back to that pre-empted thread, letting it finish with its protected code.






        share|improve this answer
























        • Make sense, perfect!!

          – Abidi
          Nov 23 '13 at 18:54














        11












        11








        11








        can context switching happen when thread is executing inside synchronized block?




        Yes, a context switch can happen inside a synchronized block as well. The only thing that's going to be different is that no other thread would be able to enter the same synchronized block (or any other block synchronized on the same object) until the context switches back to that pre-empted thread, letting it finish with its protected code.






        share|improve this answer














        can context switching happen when thread is executing inside synchronized block?




        Yes, a context switch can happen inside a synchronized block as well. The only thing that's going to be different is that no other thread would be able to enter the same synchronized block (or any other block synchronized on the same object) until the context switches back to that pre-empted thread, letting it finish with its protected code.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '13 at 13:50









        dasblinkenlightdasblinkenlight

        618k618001215




        618k618001215













        • Make sense, perfect!!

          – Abidi
          Nov 23 '13 at 18:54



















        • Make sense, perfect!!

          – Abidi
          Nov 23 '13 at 18:54

















        Make sense, perfect!!

        – Abidi
        Nov 23 '13 at 18:54





        Make sense, perfect!!

        – Abidi
        Nov 23 '13 at 18:54













        4














        What you understand is wrong. A synchronized block doesn't block other threads. A synchronized block only prevents other threads from entering a block that is synchronized on the same object. Other threads continue running while a synchronized block is being executed.



        And of course, context switching can happen while in the synchronized block, because other threads must also have some time to execute themselves. If that was not the case, two synchronized blocks running for a few seconds would block everything else on a 2-core machine. You really don't want that to happen.






        share|improve this answer
























        • My understanding of blocking of threads outside synchronized block is correct, perhaps your understanding of my question was incorrect. Thanks for your answer about context switching, it make sense.

          – Abidi
          Nov 23 '13 at 18:51











        • I can only judge your understanding by what you post. And you posted I understand a synchronized block or method will block all threads until the one inside has "left", which is clearly not what synchronized does.

          – JB Nizet
          Nov 23 '13 at 22:17
















        4














        What you understand is wrong. A synchronized block doesn't block other threads. A synchronized block only prevents other threads from entering a block that is synchronized on the same object. Other threads continue running while a synchronized block is being executed.



        And of course, context switching can happen while in the synchronized block, because other threads must also have some time to execute themselves. If that was not the case, two synchronized blocks running for a few seconds would block everything else on a 2-core machine. You really don't want that to happen.






        share|improve this answer
























        • My understanding of blocking of threads outside synchronized block is correct, perhaps your understanding of my question was incorrect. Thanks for your answer about context switching, it make sense.

          – Abidi
          Nov 23 '13 at 18:51











        • I can only judge your understanding by what you post. And you posted I understand a synchronized block or method will block all threads until the one inside has "left", which is clearly not what synchronized does.

          – JB Nizet
          Nov 23 '13 at 22:17














        4












        4








        4







        What you understand is wrong. A synchronized block doesn't block other threads. A synchronized block only prevents other threads from entering a block that is synchronized on the same object. Other threads continue running while a synchronized block is being executed.



        And of course, context switching can happen while in the synchronized block, because other threads must also have some time to execute themselves. If that was not the case, two synchronized blocks running for a few seconds would block everything else on a 2-core machine. You really don't want that to happen.






        share|improve this answer













        What you understand is wrong. A synchronized block doesn't block other threads. A synchronized block only prevents other threads from entering a block that is synchronized on the same object. Other threads continue running while a synchronized block is being executed.



        And of course, context switching can happen while in the synchronized block, because other threads must also have some time to execute themselves. If that was not the case, two synchronized blocks running for a few seconds would block everything else on a 2-core machine. You really don't want that to happen.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '13 at 13:50









        JB NizetJB Nizet

        547k588941021




        547k588941021













        • My understanding of blocking of threads outside synchronized block is correct, perhaps your understanding of my question was incorrect. Thanks for your answer about context switching, it make sense.

          – Abidi
          Nov 23 '13 at 18:51











        • I can only judge your understanding by what you post. And you posted I understand a synchronized block or method will block all threads until the one inside has "left", which is clearly not what synchronized does.

          – JB Nizet
          Nov 23 '13 at 22:17



















        • My understanding of blocking of threads outside synchronized block is correct, perhaps your understanding of my question was incorrect. Thanks for your answer about context switching, it make sense.

          – Abidi
          Nov 23 '13 at 18:51











        • I can only judge your understanding by what you post. And you posted I understand a synchronized block or method will block all threads until the one inside has "left", which is clearly not what synchronized does.

          – JB Nizet
          Nov 23 '13 at 22:17

















        My understanding of blocking of threads outside synchronized block is correct, perhaps your understanding of my question was incorrect. Thanks for your answer about context switching, it make sense.

        – Abidi
        Nov 23 '13 at 18:51





        My understanding of blocking of threads outside synchronized block is correct, perhaps your understanding of my question was incorrect. Thanks for your answer about context switching, it make sense.

        – Abidi
        Nov 23 '13 at 18:51













        I can only judge your understanding by what you post. And you posted I understand a synchronized block or method will block all threads until the one inside has "left", which is clearly not what synchronized does.

        – JB Nizet
        Nov 23 '13 at 22:17





        I can only judge your understanding by what you post. And you posted I understand a synchronized block or method will block all threads until the one inside has "left", which is clearly not what synchronized does.

        – JB Nizet
        Nov 23 '13 at 22:17











        1














        Context switch can happen inside the synchronized. Such as we can call sleep() or wait() method in synchronized block. Either sleep() or wait() make current thread unrunnable.






        share|improve this answer




























          1














          Context switch can happen inside the synchronized. Such as we can call sleep() or wait() method in synchronized block. Either sleep() or wait() make current thread unrunnable.






          share|improve this answer


























            1












            1








            1







            Context switch can happen inside the synchronized. Such as we can call sleep() or wait() method in synchronized block. Either sleep() or wait() make current thread unrunnable.






            share|improve this answer













            Context switch can happen inside the synchronized. Such as we can call sleep() or wait() method in synchronized block. Either sleep() or wait() make current thread unrunnable.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 28 '18 at 3:57









            kinglaokinglao

            827




            827























                0














                Think thread switch and synchronize are 2 complete different things. Synchronize just takes care about sharing of ressources not more not less. Another thread could do something complete different. So both are not related.






                share|improve this answer




























                  0














                  Think thread switch and synchronize are 2 complete different things. Synchronize just takes care about sharing of ressources not more not less. Another thread could do something complete different. So both are not related.






                  share|improve this answer


























                    0












                    0








                    0







                    Think thread switch and synchronize are 2 complete different things. Synchronize just takes care about sharing of ressources not more not less. Another thread could do something complete different. So both are not related.






                    share|improve this answer













                    Think thread switch and synchronize are 2 complete different things. Synchronize just takes care about sharing of ressources not more not less. Another thread could do something complete different. So both are not related.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 23 '13 at 13:54









                    RudeUrmRudeUrm

                    362




                    362























                        -2














                        The main reason that you use synchronized block is context switching done by OS actually! If there were no context switching, there were no need for a synchronized block either.



                        Actually no context switching means no thread support! Yes, it's true about MS-DOS, but not any modern OS!






                        share|improve this answer




























                          -2














                          The main reason that you use synchronized block is context switching done by OS actually! If there were no context switching, there were no need for a synchronized block either.



                          Actually no context switching means no thread support! Yes, it's true about MS-DOS, but not any modern OS!






                          share|improve this answer


























                            -2












                            -2








                            -2







                            The main reason that you use synchronized block is context switching done by OS actually! If there were no context switching, there were no need for a synchronized block either.



                            Actually no context switching means no thread support! Yes, it's true about MS-DOS, but not any modern OS!






                            share|improve this answer













                            The main reason that you use synchronized block is context switching done by OS actually! If there were no context switching, there were no need for a synchronized block either.



                            Actually no context switching means no thread support! Yes, it's true about MS-DOS, but not any modern OS!







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 23 '13 at 13:51









                            Ali HashemiAli Hashemi

                            1,65132536




                            1,65132536






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f20163056%2fin-java-can-thread-switching-happen-in-the-synchronized-block%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Guess what letter conforming each word

                                Port of Spain

                                Run scheduled task as local user group (not BUILTIN)