8

I was reading about Java's collections and then I read this line:

"None of the collection classes are synchronized, but as you will see later in this chapter, it is possible to obtain synchronized versions."

Can anyone tell me what is the difference between synchronized and non-synchronized collections in Java?

Michael
  • 41,989
  • 11
  • 82
  • 128
Incredible
  • 341
  • 2
  • 4
  • 17

5 Answers5

7

In Synchronization, If we are executing something then we need to wait for it to finish before moving to the another task.

Collection classes are not synchronized by default. The collection object is mutable that means once creating the object and that object is calling two threads at a time but one thread is changing the value of the object then it can be effected by another object. So, it is not thread safe.

We can explicitly synchronized collection using static method java.util.Collections.synchronizedCollection(Collection<T> c)

A.N.Gupta
  • 331
  • 1
  • 5
  • 16
4

A synchronized collection implies that the class is thread safe. (You can have non-synchronized collections that are also thread safe, but that is a topic for about thousand theses another day.)

The collections synchronize mutations by obtaining locks to make sure that other threads don't corrupt the state.

Basically, use the non-synchronized versions, unless you have multiple threads.

(And if you don't know, a thread is essentially a line of execution within a program. Some programs have multiple threads, all sharing the same code and memory.)

Michael
  • 41,989
  • 11
  • 82
  • 128
Paul Draper
  • 78,542
  • 46
  • 206
  • 285
2

Collection classes are not synchronized by default. But if you want a synchronized collection, you can use static method java.util.Collections.synchronizedCollection(Collection<T> c). It will create wrapper over your collection object. So, actually, your collection object will not be synchronized, but you will access your object's method via synchronized methods in wrapper object.

spgodara
  • 984
  • 9
  • 10
1

Synchronized basically means that only one thread can access methods of that particular class at any given time. StringBuffer is an example of a synchronized class. A Synchronized class is a thread-safe class.

Non-Synchronized means that two or more threads can access the methods of that particular class at any given time. StringBuilder is an example of a non-synchronized class. Generally, a non-synchronized class is not thread-safe. (but some non-synchronized classes are thread-safe)

Sahan Amarsha
  • 2,176
  • 2
  • 14
  • 23
  • I'm not convinced that just because a class is synchronized means that it's thread-safe. See: https://stackoverflow.com/q/21792030/4851565 Same thing for classes that are not synchronized. – entpnerd Feb 02 '20 at 08:09
  • Synchronized is just an one basic method of achieving thread-safe. [https://stackoverflow.com/questions/1085709/what-does-synchronized-mean/34261294#34261294] – Sahan Amarsha Feb 02 '20 at 12:43
0

Non synchronized -It is not-thread safe and can't be shared between many threads without proper synchronization code. While, Synchronized- It is thread-safe and can be shared with many threads.