This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Shared object / concurrency Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Shared object / concurrency" Watch "Shared object / concurrency" New topic
Author

Shared object / concurrency

Rafael Z. Frantz
Greenhorn

Joined: Mar 09, 2010
Posts: 20
Hi folks,

I have an object that is shared by several threads and is an observer. This object has the following methods:



Considering several threads share this object to invoke the execute() method and that the update() method is receiving thousands of notifications from the observable, do I need to synchronize the code inside mehotd update, like this:



Thanks a lot!

William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 13000
    
    5
I certainly would.

Note that there is a lot that could be going on in the Observers, you certainly don't want multiple Threads doing this "simultaneously."

Bill
Rafael Z. Frantz
Greenhorn

Joined: Mar 09, 2010
Posts: 20
Hi Bill,

I've synchronized and it seems it is working.

Thanks!
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 13000
    
    5
it seems it is working.


Well thats the tricky thing about multi-threaded programming - it is realllllly hard to test all possible sequences of events for potential collision.

Bill
 
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Shared object / concurrency
 
It's not a secret anymore!