1. What will be the output of the program? public class Q126 implements Runnable { private int x; private int y; public static void main(String [] args) { Q126 that = new Q126(); (new Thread(that)).start( ); / Line 8 / (new Thread(that)).start( ); / Line 9 / } public synchronized void run( ) / Line 11 / { for (;;) / Line 13 / { x++; y++; System.out.println("x = " + x + "y = " + y); } } }
Ask Your Doubts Here
Comments
By: guest on 02 Jun 2017 01.26 am
The synchronized code is the key to answering this question. Because x and y are both incremented inside the synchronized method they are always incremented together. Also keep in mind that the two threads share the same reference to the Q126 object. Also note that because of the infinite loop at line 13, only one thread ever gets to execute.