Why can't while(flag){} end












2















code like this:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
Thread.sleep(100)
}
println("finish")
}


the run result is :



time over
finish


The program is over



if change to:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
//Do nothing
}
println("finish")
}


"finish" cannot be print,The program is stuck. Why is that?










share|improve this question























  • try marking the flag as "@Volatile var flag = true"

    – Abhi
    Nov 21 '18 at 6:09


















2















code like this:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
Thread.sleep(100)
}
println("finish")
}


the run result is :



time over
finish


The program is over



if change to:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
//Do nothing
}
println("finish")
}


"finish" cannot be print,The program is stuck. Why is that?










share|improve this question























  • try marking the flag as "@Volatile var flag = true"

    – Abhi
    Nov 21 '18 at 6:09
















2












2








2








code like this:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
Thread.sleep(100)
}
println("finish")
}


the run result is :



time over
finish


The program is over



if change to:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
//Do nothing
}
println("finish")
}


"finish" cannot be print,The program is stuck. Why is that?










share|improve this question














code like this:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
Thread.sleep(100)
}
println("finish")
}


the run result is :



time over
finish


The program is over



if change to:



fun main(args: Array<String>) {
var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
while (flag) {
//Do nothing
}
println("finish")
}


"finish" cannot be print,The program is stuck. Why is that?







java kotlin






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 5:42









Void YoungVoid Young

235




235













  • try marking the flag as "@Volatile var flag = true"

    – Abhi
    Nov 21 '18 at 6:09





















  • try marking the flag as "@Volatile var flag = true"

    – Abhi
    Nov 21 '18 at 6:09



















try marking the flag as "@Volatile var flag = true"

– Abhi
Nov 21 '18 at 6:09







try marking the flag as "@Volatile var flag = true"

– Abhi
Nov 21 '18 at 6:09














3 Answers
3






active

oldest

votes


















5














Because you are getting a cached version of your flag. Take a look at the volatile keyword.



Basically here you are updating the flag in one thread, but the other is still seeing is own cached version of the flag.






share|improve this answer



















  • 2





    Ah, you beat me by 2 seconds for the same answer :)

    – PradyumanDixit
    Nov 21 '18 at 5:45











  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:45



















0














The problem is first thread need time to set your flag is false but while loop execute before thread finish that means still your flag is true so that its never get false.
we need to set flow after thread finish then check while condition for that use thread sleep.



var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
Thread.sleep(3000)
while (flag) {

}

println("finish")





share|improve this answer
























  • So why the "time over" is also not printing in 2nd case of OP?

    – Amit Bera
    Nov 21 '18 at 5:57











  • println("time over") is printing but not printing "finish" in 2nd case

    – sasikumar
    Nov 21 '18 at 6:02



















0














When the variable flag is not declared volatile, the compiler sometimes (depends on the one used) optimises your while loop code to the following :



if (condition) {
while (true) {
// Do nothing
}
}


Refer this link for more. Here is a similar SO question.






share|improve this answer


























  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:46











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%2f53405872%2fwhy-cant-whileflag-end%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














Because you are getting a cached version of your flag. Take a look at the volatile keyword.



Basically here you are updating the flag in one thread, but the other is still seeing is own cached version of the flag.






share|improve this answer



















  • 2





    Ah, you beat me by 2 seconds for the same answer :)

    – PradyumanDixit
    Nov 21 '18 at 5:45











  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:45
















5














Because you are getting a cached version of your flag. Take a look at the volatile keyword.



Basically here you are updating the flag in one thread, but the other is still seeing is own cached version of the flag.






share|improve this answer



















  • 2





    Ah, you beat me by 2 seconds for the same answer :)

    – PradyumanDixit
    Nov 21 '18 at 5:45











  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:45














5












5








5







Because you are getting a cached version of your flag. Take a look at the volatile keyword.



Basically here you are updating the flag in one thread, but the other is still seeing is own cached version of the flag.






share|improve this answer













Because you are getting a cached version of your flag. Take a look at the volatile keyword.



Basically here you are updating the flag in one thread, but the other is still seeing is own cached version of the flag.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 5:44









alerootaleroot

55.3k21143189




55.3k21143189








  • 2





    Ah, you beat me by 2 seconds for the same answer :)

    – PradyumanDixit
    Nov 21 '18 at 5:45











  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:45














  • 2





    Ah, you beat me by 2 seconds for the same answer :)

    – PradyumanDixit
    Nov 21 '18 at 5:45











  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:45








2




2





Ah, you beat me by 2 seconds for the same answer :)

– PradyumanDixit
Nov 21 '18 at 5:45





Ah, you beat me by 2 seconds for the same answer :)

– PradyumanDixit
Nov 21 '18 at 5:45













Thank you very much for letting me know the use of volatile

– Void Young
Nov 25 '18 at 2:45





Thank you very much for letting me know the use of volatile

– Void Young
Nov 25 '18 at 2:45













0














The problem is first thread need time to set your flag is false but while loop execute before thread finish that means still your flag is true so that its never get false.
we need to set flow after thread finish then check while condition for that use thread sleep.



var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
Thread.sleep(3000)
while (flag) {

}

println("finish")





share|improve this answer
























  • So why the "time over" is also not printing in 2nd case of OP?

    – Amit Bera
    Nov 21 '18 at 5:57











  • println("time over") is printing but not printing "finish" in 2nd case

    – sasikumar
    Nov 21 '18 at 6:02
















0














The problem is first thread need time to set your flag is false but while loop execute before thread finish that means still your flag is true so that its never get false.
we need to set flow after thread finish then check while condition for that use thread sleep.



var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
Thread.sleep(3000)
while (flag) {

}

println("finish")





share|improve this answer
























  • So why the "time over" is also not printing in 2nd case of OP?

    – Amit Bera
    Nov 21 '18 at 5:57











  • println("time over") is printing but not printing "finish" in 2nd case

    – sasikumar
    Nov 21 '18 at 6:02














0












0








0







The problem is first thread need time to set your flag is false but while loop execute before thread finish that means still your flag is true so that its never get false.
we need to set flow after thread finish then check while condition for that use thread sleep.



var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
Thread.sleep(3000)
while (flag) {

}

println("finish")





share|improve this answer













The problem is first thread need time to set your flag is false but while loop execute before thread finish that means still your flag is true so that its never get false.
we need to set flow after thread finish then check while condition for that use thread sleep.



var flag = true
Thread {
Thread.sleep(2000)
println("time over")
flag = false
}.start()
Thread.sleep(3000)
while (flag) {

}

println("finish")






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 5:49









sasikumarsasikumar

7,70011226




7,70011226













  • So why the "time over" is also not printing in 2nd case of OP?

    – Amit Bera
    Nov 21 '18 at 5:57











  • println("time over") is printing but not printing "finish" in 2nd case

    – sasikumar
    Nov 21 '18 at 6:02



















  • So why the "time over" is also not printing in 2nd case of OP?

    – Amit Bera
    Nov 21 '18 at 5:57











  • println("time over") is printing but not printing "finish" in 2nd case

    – sasikumar
    Nov 21 '18 at 6:02

















So why the "time over" is also not printing in 2nd case of OP?

– Amit Bera
Nov 21 '18 at 5:57





So why the "time over" is also not printing in 2nd case of OP?

– Amit Bera
Nov 21 '18 at 5:57













println("time over") is printing but not printing "finish" in 2nd case

– sasikumar
Nov 21 '18 at 6:02





println("time over") is printing but not printing "finish" in 2nd case

– sasikumar
Nov 21 '18 at 6:02











0














When the variable flag is not declared volatile, the compiler sometimes (depends on the one used) optimises your while loop code to the following :



if (condition) {
while (true) {
// Do nothing
}
}


Refer this link for more. Here is a similar SO question.






share|improve this answer


























  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:46
















0














When the variable flag is not declared volatile, the compiler sometimes (depends on the one used) optimises your while loop code to the following :



if (condition) {
while (true) {
// Do nothing
}
}


Refer this link for more. Here is a similar SO question.






share|improve this answer


























  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:46














0












0








0







When the variable flag is not declared volatile, the compiler sometimes (depends on the one used) optimises your while loop code to the following :



if (condition) {
while (true) {
// Do nothing
}
}


Refer this link for more. Here is a similar SO question.






share|improve this answer















When the variable flag is not declared volatile, the compiler sometimes (depends on the one used) optimises your while loop code to the following :



if (condition) {
while (true) {
// Do nothing
}
}


Refer this link for more. Here is a similar SO question.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 6:29

























answered Nov 21 '18 at 6:11









PranjalPranjal

1363




1363













  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:46



















  • Thank you very much for letting me know the use of volatile

    – Void Young
    Nov 25 '18 at 2:46

















Thank you very much for letting me know the use of volatile

– Void Young
Nov 25 '18 at 2:46





Thank you very much for letting me know the use of volatile

– Void Young
Nov 25 '18 at 2:46


















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%2f53405872%2fwhy-cant-whileflag-end%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

How to change which sound is reproduced for terminal bell?

Can I use Tabulator js library in my java Spring + Thymeleaf project?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents