Synchronization

Hi,

Please can anyone tell me when to use synchronization and when not to use with program samples?.

Thanks...

Babu Shanmugham

[151 byte] By [babusha] at [2007-11-27 11:10:04]
# 1

did u try googling? anyways, its used whenever you have a thread safety problem (precisely...)..

@@CKM@@a at 2007-7-29 13:39:22 > top of Java-index,Java Essentials,Java Programming...
# 2

for example if u have list that is used by multiple threads... so when ever u have to access that list (change or read) you have to do that in synchronized block..

synchronized (list) {

list.add("...");

}

So, it means when multiple threads are accessing a resource then you have to do synchronization to prevent ConcurrentModificationException and other concurrency problems that u do not want to face...

Nistelrooya at 2007-7-29 13:39:22 > top of Java-index,Java Essentials,Java Programming...
# 3

but synchronization gives birth to deadlock... to solve that, read about monitors... wait(), notify(), notifyAll()....

@@CKM@@a at 2007-7-29 13:39:22 > top of Java-index,Java Essentials,Java Programming...
# 4

Yes synchronization may come with other problems if not carefully implemented.. so it should be studied carefully before implementing

Nistelrooya at 2007-7-29 13:39:22 > top of Java-index,Java Essentials,Java Programming...