This is a JQ+question
Consider the following code:
public class
Test extends
Thread {
static int x, y;
public synchronized void run(){ for(;
{ x++; y++; System.out.println(x+" "+y);} }
public static void main(
String[] args)
{
new Test().start();
new Test().start();
}
}
What will the above code print?
Ans: You cannot say anything about the values.
------
This is the answer given by JQ plus.
I think the answer should be:
x and y should increment by 1 on each line
Why i think this is right because, any thread that goes into the
synchronised method will definitely increment x and y by 1 before it prints anything.
It is possible that the 2nd thread enters the synchronized method before the first thread exits out of it but , still since the variables x and y are static and they are incremented inside the infinite for loop
they should increment by 1 on each line.
Any comments on this??
Thanks in advance
Padmini