Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Create Thread-Safe Classes on Multiprocessor Hardware

 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me with this:

What techniques can be used to create thread-safe classes on multiprocessor hardware without using mutual exclusion (synchronize keyword, explicit locks or library classes that use locks)?

Thank you.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your goal here is to prevent any data updates from needing to be shared across threads. Once you need to update data in one thread and see it in another - or need to update data in more than one thread you will need one of the synchronization schemes you describe above*.

So to the point of excluding data updates: You can use Immutable classes, use different instance data per thread - such as ThreadLocal, method local variables, and different instances of Objects per thread/task.

Other than that - you would need to explain more about what you want to do and why you don't want synchronization.


*I don't know if this is true - but I am assuming volatile is excluded as well. Volatile variables will be reported across threads but are really only good at saving data integrity of a single value at a single moment in time. So it is not a solution to complex data sharing - though it can be used for 'optimistic locking' schemes which track updates via a volatile version number (for example).
 
Natalie Kopple
Ranch Hand
Posts: 325
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the very good feedback.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic