can anyone please give the output of the following with a proper explaination?
public class ThreadDemo
private int count = 1;
public synchronized void doSomething()
for (int i = 0; i < 10; i++)
public static void main(String args)
ThreadDemo demo = new ThreadDemo();
Thread a1 = new A(demo);
Thread a2 = new A(demo);
class A extends Thread
public A(ThreadDemo td)
demo = td;
public void run()
@Neeladyuti Chaudhury : More you are specific about your question better the chances you get a good response. Your post is very vague and people, who read it, cant even imagine what you are looking/asking for. Can you please rephrase the post to signify which part of it you want to understand ?
I agree with what Rakesh & Tony highlighted about your query. Below is what I understood, please repost if you asked something else.
Your code will output a sequence from 1-20. Sequence 1-10 will come from one thread and 11-20 will come from the other. It is not possible to say which thread prints which sequence as it depends on the thread scheduler.