Synchronization
February 28, 2008 by muralis
Check here about the Synchronization. When it to be used ? Under Collections Framework How the list to be synchronized ?
Synchronization is a way to let JVM know that a particular method or block of code is locked and is accessible to only one thread at a given point of time.
JVM takes care of synchronization.
Java language synchronization overview
The Java language provides three core synchronization primitives:
Synchronized methods and blocks allow a thread to lock an object on entry and unlock the object on exit (to the method or block).
Object.wait() releases the object lock and the thread waits.
Object.notify() unblocks a thread wait() on the object. notifyAll() unblocks all waiters.
Threads doing wait() and notify() must currently have locked the object.
Lock contention occurs when a thread tries to lock an object that is already locked by another thread. When this happens, the thread failing to acquire the lock is put into a logical queue of lock contenders for the object. Similarly, several threads could have done Object.wait() on the same object, and so there’s a logical queue of waiters for the object.
Basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList?
The basic difference between a Vector and an ArrayList is that, vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized data structure will give better performance than the synchronized one.