Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

mutex in java  RSS feed

 
vadz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How mutex is used in java, can primitive data types can be mutexed?

Thanx in advance
 
Timmy Marks
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mutexes are realized in Java by simply declaring the method (or portion of a method) that should be mutually excluded as synchronized. This will prevent any other thread from executing any other synchronized block on the object that is synchronized.

As for mutexing on a primitive, just wrap it in a wrapper class.
 
vadz
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marks,
Can you give an code snippet for this ?


Thanks
vadz
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand you correctly, then then answer would be:
You can't call "synchronized" on a primitive type.
E.g:


What you can do, is declare some dummy object and remember to lock it every time you access your "x".
E.g:


Of course, with object-oriented desing, you often end up with "x" being stored in some object anyway.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!