aspose file tools*
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Que on thread" Watch "Que on thread" New topic
Author

Que on thread

sanjana narayanan
Ranch Hand

Joined: Nov 25, 2003
Posts: 142
Hi,
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
sureshs
babua
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?
-Sanjana
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
Sureshs
babua
In second run
SureshsM
babuaN
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Que on thread
 
Similar Threads
Threads
Inner Thread Classes
Can this be a case of deadlock ?And how to determine it output ?
Threads-synchronized blocks,Valentine &Michael and Others,Help needed
more fun Qestions, thread=)