views:

947

answers:

4

Vector methods are synchronized. What does it mean programmatically and logically?

+2  A: 

To my understanding synchronized basically means that the compiler write a monitor.enter and monitor.exit around your method. As such it may be thread safe depending on how it is used (what I mean is you can write an object with synchronized methods that isn't threadsafe depending on what your class does).

Spence
+14  A: 

The synchronized keyword is all about different threads reading and writing to the same variables, objects and resources. This is not a trivial topic in Java, but here is a quote from Sun:

Synchronized methods enable a simple strategy for preventing thread interference and memory consistency errors: if an object is visible to more than one thread, all reads or writes to that object's variables are done through synchronized methods.

In a very, very small nutshell: When you have two threads that are reading and writing to the same 'resource', say a variable named foo, you need to ensure that these threads access the variable in an atomic way. Without the synchronized keyword, your thread 1 may not see the change thread 2 made to foo, or worse, it may only be half changed. This would not be what you logically expect.

Again, this is a non-trivial topic in Java. To learn more, explore topics here on SO and the Interwebs about:

Keep exploring these topics until the name "Brian Goetz" becomes permanently associated with the term "concurrency" in your brain.

Stu Thompson
So, basically this Synchronized keyword makes your methods thread-safe?
Rigo Vides
The synchronized keyword is one of the tools that make your code thread safe. Just using synchronized on a method or variable in itself may or may not do the trick. Having a basic understanding of the Java Memory Model is really important to getting concurrency correct.
Stu Thompson
Unless you are Brian Goetz (or maybe Jon Skeet), it is almost impossible to get Java concurrency correct with only the language primitives (synchronized, volatile). For starters, make use of the java.util.concurrent package and build on top of that.
Thilo
Some of us did code (presumably) thread-safe code with Java 1.4, thank you very much ;-).
Robert Munteanu
+2  A: 

The synchronized keyword causes a thread to obtain a lock when entering the method, so that only one thread can execute the method at the same time (for the given object instance, unless it is a static method).

This is frequently called making the class thread-safe, but I would say this is a euphemism. While it is true that synchronization protects the internal state of the Vector from getting corrupted, this does not usually help the user of Vector much.

Consider this:

 if (vector.isEmpty()){
     vector.add(data);
 }

Even though the methods involved are synchronized, because they are being locked and unlocked individually, two unfortunately timed threads can create a vector with two elements.

So in effect, you have to synchronize in your application code as well.

Because method-level synchronization is a) expensive when you don't need it and b) insufficient when you need synchronization, there are now un-synchronized replacements (ArrayList in the case of Vector).

More recently, the concurrency package has been released, with a number of clever utilities that take care of multi-threading issues.

Thilo
+1  A: 

Think of it as a kind of turnstile like you might find at a football ground. There are parallel steams of people wanting to get in but at the turnstile they are 'synchronised'. Only one person at a time can get through. All those wanting to get through will do, but they may have to wait until they can go through.

paul