• 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Dan's Question about 2 Threads

Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I don't understand the possible reuslts of the following question of Dan's study guide.

What are the possible results of attempting to compile and run the program?
a. Prints nothing.
b. Prints: [T1,A][T2,B].
c. Prints: [T1,B][T2,B].
d. Prints: [T2,B][T1,A].
e. Prints: [T2,A][T1,A].
f. Compiler error.
g. Run time error.
h. None of the above.
The right answers are b and d. I tried it out and this is right.
But I don't understand it.
There is only one instance of class B.
Class B has a member variable called s1, which is set in the synchronized method m1.
If this variable is set to "A" or to "B" and the yield method is called, I think it must be overwritten in the next call of m1 of the second thread.
So I expected, that the possbile results are c and e.
But this does not happen.
Can anybody explain this to me ?
Thanks in advance.
[ Jess added UBB [code] tags to preserve whitespace, check 'em out! ]
[ September 08, 2003: Message edited by: Jessica Sant ]
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael
It's so because in here the method is synchronized. This would ensure that the second method invocation is done only after the first has completed. The yield() method according to me won't affect the execution I guess.
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, it's because the run() method in A class is not synchronized, so there's no guarantee which thread runs first. Secondly, when a Thread enters m1() method of B, an attempt to yield() is made to let a thread of the same or higher priority run. I don't believe this attempt is successful because we're inside the synchronized method of the only object of B, and the lock should be relinquished. So to me, the output varies from b. to d. due to run() not being synchronized and just one instance of B.
Please correct me, this question looks interesting.
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, does anybody think any different on this one?
This is my favorite show. And this is my favorite tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic