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
posted 12 years ago
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