Java Programming - Threads - Discussion

Discussion Forum : Threads - Finding the output (Q.No. 7)
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(); 
    } 
}
DeadLock
It print 12 12 12 12
Compilation Error
Cannot determine output.
Answer: Option
Explanation:

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

Discussion:
15 comments Page 2 of 2.

Rakesh said:   1 decade ago
Explain the usage of synchronized?

Lefteris said:   9 years ago
This cause a compilation error as NO class in Java can start with a lowercase letter!

Please help me.

Ayush said:   9 years ago
The for loop is not having braces so the synch block is in for loop and other code is not in loop.

Pavan said:   9 years ago
@Lefteris.

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

Mohammed Parvez said:   5 years ago
For loop is a trap, If you provide braces for 'for' loop then it prints as much as condition.
(1)


Post your comments here:

Your comments will be displayed after verification.