permaculture playing cards
The moose likes Threads and Synchronization and the fly likes thread output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "thread output" Watch "thread output" New topic

thread output

Neeladyuti Chaudhury

Joined: Nov 26, 2012
Posts: 1

Hi ,

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
ThreadDemo demo;
public A(ThreadDemo td)
demo = td;
public void run()

Tony Docherty

Joined: Aug 07, 2007
Posts: 2838
Welcome to the ranch.

Please use code tags when posting code.

can anyone please give the output of the following with a proper explaination?

Have you tried running it multiple times to see what happens?
Rakesh K. Cherukuri
Ranch Hand

Joined: Jun 01, 2010
Posts: 48

@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 ?

Warm Regards,
Sarfarajey Akram

Joined: Oct 16, 2012
Posts: 4

Welcome to Ranch

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.
I agree. Here's the link:
subject: thread output
It's not a secret anymore!