In Java, can thread switching happen in the synchronized block?
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
add a comment |
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
2
Yes, context switching can happen as it is handled by OS and not JVM
– Nandkumar Tekale
Nov 23 '13 at 13:47
add a comment |
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
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
java multithreading
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
add a comment |
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
add a comment |
5 Answers
5
active
oldest
votes
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.
Make sense, perfect!!
– Abidi
Nov 23 '13 at 18:54
add a comment |
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.
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
add a comment |
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.
add a comment |
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.
add a comment |
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!
add a comment |
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
});
}
});
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
Required, but never shown
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
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.
Make sense, perfect!!
– Abidi
Nov 23 '13 at 18:54
add a comment |
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.
Make sense, perfect!!
– Abidi
Nov 23 '13 at 18:54
add a comment |
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.
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.
answered Nov 23 '13 at 13:50
dasblinkenlightdasblinkenlight
618k618001215
618k618001215
Make sense, perfect!!
– Abidi
Nov 23 '13 at 18:54
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Feb 28 '18 at 3:57
kinglaokinglao
827
827
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 23 '13 at 13:54
RudeUrmRudeUrm
362
362
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Nov 23 '13 at 13:51
Ali HashemiAli Hashemi
1,65132536
1,65132536
add a comment |
add a comment |
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.
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
Required, but never shown
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
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
Required, but never shown
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
Required, but never shown
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
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
2
Yes, context switching can happen as it is handled by OS and not JVM
– Nandkumar Tekale
Nov 23 '13 at 13:47