File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Que on thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Que on thread" Watch "Que on thread" New topic

Que on thread

sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
This program was already posted but i have more questions on the program.

1.How many threads are created in the program?
2. Output is
In first run
In second run
sureshM // why is sureshM and not sureshsM
babuaN // here N is getting appended to babua and not to babu
Not sure abt the output.

Any explanation would be appreciated?
David Hadiprijanto
Ranch Hand

Joined: Sep 14, 2003
Posts: 52
Hi Sanjana,

1. How many threads are created in the program?

I think there are 3 threads created, on line 5, 10, and 11.
[I re-pasted your code below, it was rather difficult to read your posted code ]
About the output, I don't think it is possible to get the output as you described. Since both the s1.append(); and s2.append(); (line 1, 2, 6, and 7) are synchronized using s1 and s2, if you get Sureshs then you should get babua, SureshsM, and babuaN. Another possibility is you get SureshM, then you will get babuN, SureshMs, and babuNa - if "In second run" thread get to line 6 before "In first run" thread get to line 1. (Using the Thread.sleep method on line 0 to force the "In first run" thread to execute the s1.append() after "In second run" thread).
When I try your code (without the Thread.sleep on line 0), I happen to get :
In first run
In second run
I agree. Here's the link:
subject: Que on thread
It's not a secret anymore!