Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between synchronized and non-synchronized collection classes in Java?

Tags:

java

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?

like image 601
Incredible Avatar asked Feb 02 '14 06:02

Incredible


1 Answers

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)

like image 109
A.N.Gupta Avatar answered Oct 20 '22 00:10

A.N.Gupta