1. 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(); (new Thread(that)).start(); (new Thread(that)).start(); } public synchronized void run() { for(int i = 0; i < 10; i++) { x++; y++; System.out.println("x = " + x + ", y = " + y); / Line 17 / } } }
Ask Your Doubts Here
Comments
By: guest on 02 Jun 2017 01.26 am
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);