• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Thread problem.

 
Li Jishan
Greenhorn
Posts: 5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all!

A) Compile time Error There is no start method
B) Will print in this order 11 22 33
C) Will print but not exactly in an order (eg: 123124 3)
D) Will print in this order 12 3123
E) Will print in this order 123.
The answer is C) why? or the answer is wrong? I ran it in JDK 1.2.2 for tons of times, the result is 11 22 33 ... 2020
Who can explain how t1 & t2 run?
Thanks!
-Li Jishan
 
Amir Ghahrai
Ranch Hand
Posts: 110
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your code above, the methods addX() and addY() are not synchronized, therefore they can be executed by more than one thread at a time.
Only when you synchronize the methods you can guarantee that it will give the same result everytime it runs.
Also remember that thread scheduler, is platform dependant, therefore you can see the same output on your machine everytime you run the program, but if you move to another OS, you may get different result. that's why answer C is correct.
hope that helps!
I think answer C should be re-worded as
Will print but not necessarily in an order (eg: 123124 3)
[ July 28, 2002: Message edited by: Amir Ghahrai ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic