• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Que on thread

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic