File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Thread Safe Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread Safe" Watch "Thread Safe" New topic
Author

Thread Safe

Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Methods in vector and hashtable are Thread Safe, since its methods are synchronized...
Does it mean two different thread cannot access the objects stored in vector simultaneously???
Please give your valuable suggestion...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Naresh Shanmugam wrote:Does it mean two different thread cannot access the objects stored in vector simultaneously???

Yes.
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Consider "Vector" has two objects in it, "aObject" and "bObject". So thread safe ensures that two threads(thread1, thread2) cannot parallely update "aObject"/"bObject", but thread1 can update "aObject" parallely when thread2 updates "bObject". If this understanding is correct, why are we telling it reduces the performance,
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
thread1 can update "aObject" parallely when thread2 updates "bObject"

don't confuse multithreading with parallelism. Vector is designed for multithreaded access (multiple threads are able to use the same Vector instance safely), but it is not suitable for parallel (concurrent) processing, i.e. multiple threads cannot access it's methods simultaneously.
ThreadOne cannot call vector.get(0) while ThreadTwo calling vector.get(1) - someone will have to wait. In fact there's no way to execute any Vector methods in parallel.
And it is slower that ArrayList for example because synchronization means some additional overhead associated with acquiring the lock. So ArrayList works faster but it cannot be accessed by multiple threads at all (unless you provide some external locking).

SCJP, SCWCD
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18550
    
  40

Garik Ustinov wrote:
ThreadOne cannot call vector.get(0) while ThreadTwo calling vector.get(1) - someone will have to wait. In fact there's no way to execute any Vector methods in parallel.


I thought that the example provided (and asked about) was about two different instances, aObject and bObject, which can be used by different threads in parallel... since they are different instances and using different locks.

Garik Ustinov wrote:
And it is slower that ArrayList for example because synchronization means some additional overhead associated with acquiring the lock. So ArrayList works faster but it cannot be accessed by multiple threads at all (unless you provide some external locking).


Since Java 5, a ton of work has been done to lower the time to grab an uncontended lock. It is definitely in the order of less than 100 nanos. In most cases, this is probably not noticeable.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Naresh Shanmugam wrote:Consider "Vector" has two objects in it, "aObject" and "bObject". So thread safe ensures that two threads(thread1, thread2) cannot parallely update "aObject"/"bObject", but thread1 can update "aObject" parallely when thread2 updates "bObject". If this understanding is correct, why are we telling it reduces the performance,


No, Vector's method synchronized on this, means, they are synchronized on that Vector object itself, not on the objects which are saved on that Vector object!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Garik Ustinov
Ranch Hand

Joined: Jun 22, 2009
Posts: 31
Henry Wong wrote:
I thought that the example provided (and asked about) was about two different instances, aObject and bObject

Well, I was talking about accessing the shared Vector by multiple threads. Apologies if any confusion caused

Henry Wong wrote:
It is definitely in the order of less than 100 nanos.

Indeed. But I thought the performance question was "Why" not "How bad"
Still, it's a bad idea to use Vectors throughout the application if performance matters since even nanoseconds tend to accumulate.
Naresh Shanmugam
Ranch Hand

Joined: Jul 16, 2010
Posts: 84
Thank you all for your valuable time, i got the point...
Vinoth Kumar Kannan
Ranch Hand

Joined: Aug 19, 2009
Posts: 276

Abimaran Kugathasan wrote:
No, Vector's method synchronized on this, means, they are synchronized on that Vector object itself, not on the objects which are saved on that Vector object!

This makes me think.., how to synchronize 'this' ??
Abimaran,you mean, any method on Vector class can be used by only one instance at a time? that is, if an instance of Vector is accessing get() all other instances have to wait till it leaves the method thereby unlocking the resource & similarly for all its methods?


OCPJP 6
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Vinoth Kumar Kannan wrote:
This makes me think.., how to synchronize 'this' ??
Abimaran,you mean, any method on Vector class can be used by only one instance at a time? that is, if an instance of Vector is accessing get() all other instances have to wait till it leaves the method thereby unlocking the resource & similarly for all its methods?

In a multi-threaded application, if you use ArrayList, one Thread can read the ArrayList while the other can update the same ArrayList, but for vectors, if a thread updating a vector instance, no other thread can't access that vector instance.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread Safe
 
Similar Threads
StringBuffer & StringBuilder and equals()
how is Vector Class Thread Safe
Why Vector is made as a Legacy Class
why some classes are called deprecated & some legacy?
what is the difference between the ArrayList and Vector?