單項選擇題

public class X implements Runnable { 
private int x; 
private int y; 
public static void main(String [] args) { 
X that = new X(); 
(new Thread( that )).start(); 
(new Thread( that )).start(); 
} 
public void run() { 
for (;;) { 
synchronized (this) { 
x++; 
y++; 
} 
System.out.println(Thread.currentThread().getName() + 
“x = “ + x + “, y = “ + y); 
} 
} 
}  
What is the result?()  

A. Compilation fails.
B. The program prints pairs of values for x and y that might not always be the same on the same line (for example, “x = 2, y = 1”).
C. The program prints pairs of values for x and y that are always the same on the same line (for example, “x = 1, y = 1”).    In addition, each value appears only once (for example, “x = 1, y = 1” followed by “x = 2, y = 2”).    The thread name at the start of the line shows that both threads are executing concurrently.
D. The program prints pairs of values for x and y that are always the same on the same line (for example, “x = 1, y = 1”).     In addition, each value appears only once (for example, “x = 1, y = 1” followed by “x = 2, y = 2”).    The thread name at the start of the line shows that only a single thread is actually executing.

微信掃碼免費搜題