"I never see what has been done; I only see what remains to be done."
- Marie Curie
What will be the output of the program?
public class Test107 implements Runnable
private int x;
private int y;
public static void main(String args)
Test107 that = new Test107();
public synchronized void run()
for(int i = 0; i < 10; i++)
System.out.println("x = " + x + ", y = " + y); /* Line 17 */
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by both threads running simultaneously.
Will print in this order: x = 1 y = 1 x = 2 y = 2 x = 3 y = 3 x = 4 y = 4 x = 5 y = 5... but the output will be produced by first one thread then the other. This is guaranteed by the synchronised code.
Will print in this order x = 1 y = 2 x = 3 y = 4 x = 5 y = 6 x = 7 y = 8...
Answer: Option D
Both threads are operating on the same instance variables. Because the code is synchronized the first thread will complete before the second thread begins. Modify line 17 to print the thread names:
System.out.println(Thread.currentThread().getName() + " x = " + x + ", y = " + y);