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);
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);
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
.
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.