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();
}
}
Answer: Option
Explanation:
The program will execute without any problems and print 12 12 12 12.
Discussion:
15 comments Page 2 of 2.
Sunil said:
1 decade ago
If I remove synchronize block then what happen ?
Sanchi said:
2 decades ago
It is good and easy to understand.
Abhinav said:
1 decade ago
Where is the body of the for loop?
Rakesh said:
1 decade ago
Explain the usage of synchronized?
Pramod said:
1 decade ago
Please explain me whole code.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers