i hava a qeustion about debug in intellij

Answered

firstly, i am not good at english ! sorry!

I am a university student and I am studying java now!

I have a one question about example in under picture.

I expect the value of "button" in backgroundThread is always false.

so I expect to it will be occur infinite loop.

but, when I debug it, it is not seems to be infinite loop. 

I want to know why it is not infinite loop while debug.

thank you !

0
5 comments
Avatar
Yaroslav Bedrov

Hello,

Your example also exits in run mode. Have you tried to run it from command line?

1

yes, I have tried that!

When I run it from command line, I could not get any output.  (it means, i can not break while )

But, when I run it from debug mode, I can reach the break point under while.

0
Avatar
Yaroslav Bedrov

Sorry for the delay.

Your "button" variable is not  volatile (https://stackoverflow.com/questions/106591/what-is-the-volatile-keyword-useful-for) and there is no threads synchronization. So, actually at some random moment other thread will see that "button" is true now and exit from the loop. In debug mode it happens faster because some java optimizations are disabled.

0

Thank you for your reply!

0
Avatar
Yaroslav Bedrov

You are welcome!

0

Please sign in to leave a comment.