Win a copy of Spring Boot in Practice this week in the Spring forum!
  • 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Thread problem.

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic