Java Programming - Threads - Discussion

5. 

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); 
        } 
    } 
}

[A]. An error at line 11 causes compilation to fail
[B]. Errors at lines 8 and 9 cause compilation to fail.
[C]. 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")
[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 once (for example, "x=1, y=1" followed by "x=2, y=2")

Answer: Option D

Explanation:

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.


Alex said: (Jan 25, 2012)  
How it will work D-way if 'x' and 'y' are never initialized and are private, so there's no access to set them out of this class?

Not to mention comma and space (", ") appeared in your answer in example output.

Thanks, Alex.

Alex said: (Jan 25, 2012)  
Oh, I got it - they are int primitives, zero by default. :)

Vasavi said: (Jun 17, 2013)  
Why here no error will come? because here we cal start() method 2 times with saMe object.

Vasavi said: (Jun 17, 2013)  
What is the use of synchronized keyword? what it work's here?

Austin said: (Jul 31, 2013)  
Due to the header of run method, the run() method of interface runnable will not be overridden. Therefore, there will be method name mismatch, i.e., it will be neither overloaded or overridden.

Hence it should give compilation error.

Savitha said: (Aug 15, 2013)  
Austin, synchronized keyword is not part of the method signature. So it can be added for overriding a method. Also, a synchronized method can be overridden without using a synchronized keyword. So you would not get a compilation error for the above code

As there is a infinite loop, the first thread enters execution , keeps holding the lock, so the next thread never gets its turn.

Suvarna said: (Sep 29, 2013)  
I have executed this code. I found exception constructor thread is undefined.

Dota 2 said: (Feb 17, 2014)  
Java should have initial value for each variable. On this code x and y don't have it.

Rupesh Pawar said: (Oct 13, 2014)  
How come its fine to use start() method twice in this program. ?

Abc said: (Oct 18, 2014)  
How come it does not give an error?

There is no that keyword in JAVA!

Preeti said: (Dec 2, 2016)  
@Rupesh Pawar.

It is ok to start twice as a new thread is created second time so both will act ss diff thread for the same object.

Pavan said: (Dec 19, 2016)  
@Abc.

That is the reference of the object Q126.

Narendra said: (Jun 27, 2017)  
I have executed code and two threads got a chance.

Thread-1x = 219751y = 219751
Thread-1x = 219752y = 219752
Thread-1x = 219753y = 219753
Thread-0x = 68569y = 68569
Thread-1x = 219754y = 219754
Thread-1x = 219755y = 219755
Thread-1x = 219756y = 219756
Thread-1x = 219757y = 219757
Thread-1x = 219758y = 219758
Thread-0x = 68570y = 68570
Thread-0x = 68571y = 68571

Can someone answer this?

Varsha said: (Jan 6, 2018)  
Start() method called two times with same object so it should give error.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.