File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Threads and Synchronization and the fly likes where we have to synchronize???? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "where we have to synchronize????" Watch "where we have to synchronize????" New topic

where we have to synchronize????

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi all,

in program,where we need to synchronize?

in which scenario syncronization is need?

i study some tutorial,still i confused..

please anyone can explain me with real time example..

Thanks & Regards,
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1294

Hi seetharaman,

in short you need synchronization when using multiple threads in an application. Synchronization is needed to prevent unexpected access to shared data among multiple threads. Without proper synchronization you have good chances for corrupted data and kind of non-deterministic behavior of your program.

But multi-threading and correct synchronization is really a complex topic and you should search for tutorials or documentation on it. Unfortunately it's simply not possible to explain this here in a few sentences.

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Marco
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
While Marco's answer is spot on, I'd like to add one little detail.

Specifically, you need to synchronize if you have shared mutable data. If the data that's shared between threads is immutable -meaning it does not change during it's lifetime, or if you can guarantee that it does not change when there's a chance of concurrent access- then there's no need for synchronization.
[ May 28, 2008: Message edited by: Ulf Dittmer ]
vinayak katkar

Joined: May 28, 2008
Posts: 2
Synchronize key word is used with methods or block of code in methods
It is used where the block of code is sharing common resources

�Knowledge is the food of the soul.�<br /> <br /><br />To get free knowledge,ebook links, free pdf ,and new technology discussions visit the following link <br /><a href="" target="_blank" rel="nofollow"></a>
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks ULF and VINAYAK
I agree. Here's the link:
subject: where we have to synchronize????
It's not a secret anymore!