1

I'm trying to figure out what are synchronized on the Java collection framework. But still haven’t got any clear solution.

I mean, if we get

  • list
  • Queie
  • Set

And on the list

  • ArrayList
  • LinkedList
  • Vector

What are the synchronized?

If we get HashMap and HashTable we know Hashtable is synchronized on the table while access to the HashMap isn't.

MadukaJ
  • 722
  • 6
  • 22
  • 4
    please read about http://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html – Simulant Jun 30 '14 at 11:09
  • http://stackoverflow.com/questions/6045648/which-all-java-collections-are-synchronized-and-not-synchronized – Mustafa sabir Jun 30 '14 at 11:09
  • 1
    I faced an interview and they asked “what's synchronized in the collection framework?” that’s why I'm looking for this. – MadukaJ Jun 30 '14 at 11:22

7 Answers7

3

Take a look on the following utility methods:

  • Collections.synchronizedCollection()
  • Collections.synchronizedList()
  • Collections.synchronizedSet()
  • Collections.synchronizedMap()
AlexR
  • 114,158
  • 16
  • 130
  • 208
3

Not a single implementation of the collection is synchronized because synchronized is not a class property, it is only applicable to methods and blocks.

User1100
  • 45
  • 4
3

Each class in the jdk collection api documents whether it is thread safe or not. Older classes like java.util.Vector tended to be synchronized on every method, until they became replaced with a non synchronized version. ArrayList in this case. Then the concurrent package was added, and everything in there had a thread safety strategy of one kind or another. In general though, if the class documentation does not say that the class is thread safe then treat it as not being thread safe.

Chris K
  • 11,622
  • 1
  • 36
  • 49
2

When you write

private Object syncObject = new Object();

public void someFunction(Stuff stuff)
{
     synchronized(syncObject)
     {
         list.add(stuff);
     }
}

public void someOtherFunction()
{
     synchronized(syncObject)
     {
         for(Stuff stuff : list)
         {
             stuff.doStuff();
         }
     }
}

Then what it means is that the Monitor of the syncObject object does not allow multiple threads inside it, it allows only a single Thread into that object's monitor. This is called mutual exclusion, http://en.wikipedia.org/wiki/Mutual_exclusion

This is basically so that if you have multiple threads, then you can make execute specific code blocks only one thread at a time. For example, while you iterate the array through one, but you're adding items to that array in another, and you're removing in a third. You don't want them to mess with each other, because that can create inconsistent results.

The function

Collections.synchronizedCollection(Collection<T> c)

creates a Decorator around the Collection to make its methods synchronized, for example.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
1

"Synchronized" means the methods of that class are synchronized. All the legacy classes(e.g. Vector, Hashtable, Stack) are considered to be synchronized. More specifically, the personal methods of these legacy classes are synchronized. But now take an example of Vector class, Vector implements Collection interface hence it contains some utility methods from Collection interface(e.g. add(), remove() etc.), and these methods are not synchronized.

Suyash Soni
  • 219
  • 2
  • 9
1

all legacy classes are the synchronize classes it means thread safe classes. Vector and Stack are the synchronize and Enumeration is used to access the values in synchronize class.

Piyush Mittal
  • 1,860
  • 1
  • 21
  • 39
0

Legacy classes are synchronized like Vector, Stack, HashMap. You also can check if all the methods of a particular class are synchronized or not like

javap java.util.HashMap

Which is not synchronized class

javap java.util.Hashtable

Which is synchronized as all the methods are synchronized of Hashtable