File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

nikos thread question

 
Raju Champaklal
Ranch Hand
Posts: 521
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


why is the answer not guarenteed to be 2000? arent setNumber and getNumber synchronized...is this becasue both the threads have entered the run method and started the loop...and once the first thread finishes with setNumber and getNumber the other one can start it enter too...........?
 
Martin Sergeant
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that although the setNumber and getNumber methods are synchronized,
the action of getting and setting the number is not atomic. Therfore it is possible
for a thead call getNumber and before it has time to call setnumber , another thread
may nip in and call getNumber, thus both threads will call setnumber using the same
number. This can be avoided by combining setNumber and getNumber in one
synchronized method


Martin
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic