permaculture playing cards
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread problem. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread problem." Watch "Thread problem." New topic

Thread problem.

Li Jishan

Joined: Jul 06, 2002
Posts: 5
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?
-Li Jishan

Li Jishan
Amir Ghahrai
Ranch Hand

Joined: Jun 19, 2002
Posts: 110
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 ]

I agree. Here's the link:
subject: Thread problem.
It's not a secret anymore!