0

If thread is executing following statement (suppose value of x is 1) and sleeps at point 'Here' and some other thread changes value of x.(suppose to 10) what will be the output?

System.out.println(/Here/++x);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156
Ruturaj
  • 630
  • 1
  • 6
  • 20
  • You may want to look at answers for [Synchronization and System.out.println](http://stackoverflow.com/questions/9459657/synchronization-and-system-out-println) post. – PM 77-1 Oct 28 '13 at 18:50
  • This depends on the declaration and actual reference of x in the context of every thread – MRalwasser Oct 28 '13 at 18:54

2 Answers2

0

If the thread running the System.out stops before it does any processing on the ++x and then the value of x is changed on another thread to 10, the printed statement will be whatever Here evaluates to, followed by 11.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
0

Which ever one comes first. But you should know that there is no way it could change if the command is being executed. Even if the system is multi-core it will first finish the command and then jump on to the next. Except if it calls a big function, which is a different topic.

Sindorej
  • 181
  • 2
  • 12