Java Programming - Threads - Discussion

7. 

What will be the output of the program?

class s implements Runnable 
{ 
    int x, y; 
    public void run() 
    { 
        for(int i = 0; i < 1000; i++) 
            synchronized(this) 
            { 
                x = 12; 
                y = 12; 
            } 
        System.out.print(x + " " + y + " "); 
    } 
    public static void main(String args[]) 
    { 
        s run = new s(); 
        Thread t1 = new Thread(run); 
        Thread t2 = new Thread(run); 
        t1.start(); 
        t2.start(); 
    } 
}

[A]. DeadLock
[B]. It print 12 12 12 12
[C]. Compilation Error
[D]. Cannot determine output.

Answer: Option B

Explanation:

The program will execute without any problems and print 12 12 12 12.


Sanchi said: (Jul 8, 2010)  
It is good and easy to understand.

Kunal said: (Jul 28, 2011)  
Could you please explain how the flow will work here ?

Abhinav said: (Oct 25, 2011)  
Where is the body of the for loop?

Pramod said: (Dec 10, 2011)  
Please explain me whole code.

Robin said: (Jul 7, 2012)  
Just run this code you can understand it.

class s implements Runnable
{
int x, y;
public void run()
{
for(int i = 0; i < 1000; i++)

synchronized(this)
{
System.out.println(i);//this code execute 1000 //times ie this block synchronized 1000 time
x = 12;
y = 12;
}
System.out.print(x + " " + y + " ");// but for loop has //no effect on this block.
}
public static void main(String args[])
{
s run = new s();
Thread t1 = new Thread(run);
Thread t2 = new Thread(run);
t1.start();
t2.start();
}
}

Pravin Chavan said: (Aug 13, 2012)  
Hello guys.

There only 2 threads are present. So when the 1st thread are in run method then it print 12 and 12 at that point the 2nd thread is in runnable state (i.e he is weighting for the control of run method) , when the thread1 finished its then thread2 is access the run method. And it prints 12, 12.

So o/p= 12, 12, 12, 12.

Vishal said: (Jan 22, 2013)  
Hello Guys

Here for loop is not having any relation with System.out.print(x + " " + y + " "); after iterating for 1000 times only once this line of code will be executed by thread1,and once the lock is released thread2 will print the System.out.print(x + " " + y + " "); after iterating 1000 times So o/p will between
12 12 12 12

Sunil said: (Sep 29, 2013)  
If I remove synchronize block then what happen ?

Aghnar said: (Nov 7, 2013)  
The synchronized block is a trap. Actually the two threads can
execute the System.out.print(x + " " + y + " ");

So if this code 'System.out.print(x + " " + y + " ")' is not thread safe (which I think is possible, I have to look into the source), the result can be indeterminate.

Rochet2 said: (Jun 5, 2014)  
@Aghnar just spoke out my mind.

I looked into it a bit and it seems that the documentation did not say it was thread safe.

It can be that the implementation of it /may/ still be, but should not trust assumptions.

Rakesh said: (Mar 22, 2015)  
Explain the usage of synchronized?

Lefteris said: (May 6, 2016)  
This cause a compilation error as NO class in Java can start with a lowercase letter!

Please help me.

Ayush said: (May 11, 2016)  
The for loop is not having braces so the synch block is in for loop and other code is not in loop.

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

You can declare it with lower case, but the convention is to start with a capital letter.

Mohammed Parvez said: (Aug 18, 2020)  
For loop is a trap, If you provide braces for 'for' loop then it prints as much as condition.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.