What happens after the compilation and execution of this code?

public class S1 implements Runnable {
    int x = 0, y = 0;

    int addX() {
        x++;
        return x;
    }

    int addY() {
        y++;
        return y;
    }

    public void run() {
        synchronized (this) {
            for (int i = 0; i < 10; i++)
                System.out.print(addX() + " " + addY() + " ");
        }
    }

    public static void main(String args[]) {
        S1 run = new S1();
        Thread t1 = new Thread(run);
        Thread t2 = new Thread(run);
        t1.start();
        t2.start();
    }
}

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback