aspose file tools*
The moose likes Threads and Synchronization and the fly likes Create Thread-Safe Classes on Multiprocessor Hardware Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Create Thread-Safe Classes on Multiprocessor Hardware " Watch "Create Thread-Safe Classes on Multiprocessor Hardware " New topic
Author

Create Thread-Safe Classes on Multiprocessor Hardware

Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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).


Steve
Natalie Kopple
Ranch Hand

Joined: May 06, 2003
Posts: 325
Thank you for the very good feedback.
 
 
subject: Create Thread-Safe Classes on Multiprocessor Hardware