Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized not locked?!

 
Borus Kung
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code listed below is my exercise.
I try to test how synchronize work.
but my result shocked me,
like this:
3
which = 1shareVal=3inside show, i = 0
4
which = 2shareVal=4inside show, i = 0
wake up
which = 2shareVal=4inside show, i = 1
wake up
which = 1shareVal=4inside show, i = 1
wake up
which = 2shareVal=4inside show, i = 2
that means 2 threads in show()
I dont know what is wrong with my program.


[ July 15, 2002: Message edited by: Jim Yingst ]
 
Borus Kung
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont know why the source listed so messy, anyone can tell me how to list it in a better way?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I added code tags, which preserve your indentation. Also, please take a look at our display name policy and edit your display name to include a first and last name. Thanks.
In your code, you have synchronized the instance method show(). This means that if two thread use the same instance to access that method at the same time, one will have to wait. But this never happens in your code anyway - the two threads you create are using separate instances of NonSync, so they have no problem acquiring locks on those instances.
However, the static field shareVal is not protected by synchronizing instance methods. To protect it, you must either access it using a static synchronized getShareVal or setShareVal method, or create a synchronized block which synchronizes on the NonSync class rather than an individual instance:

Remember, when you declare a nonstatic method synchronized, you're synchronizing on the instance, which is good for protecting access to instance variables, but not static variables. When you declare a static method synchronized, you are synchronizing on the whole class, which is good for protecting static variables, but not instance variables. If you must change both static and nonstatic variables, best to use separate methods.
[ July 15, 2002: Message edited by: Jim Yingst ]
 
Borus Kung
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You very much.
I will test it now~


[ July 16, 2002: Message edited by: Borus Kung ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For what it's worth, you can also edit or delete your own posts after you make them. Just click the little pencil-and-paper icon next to your post. Thus you can experiment all you want, and simply delete it afterwards. Or fix mistakes after the fact. Enjoy...
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Simply making the method show static should solve your problem. Or at least validate what the prior poster said about your synchronizing on 2 seperate objects.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic