Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean when we say Hashtable or Vector is synchronized?

Tags:

java

The questions says it all, just wondering, in an interview, what would you say when they ask you, "What does it practically mean by Hashtable or Vectors being synchronized?"

like image 301
zengr Avatar asked Apr 08 '10 16:04

zengr


People also ask

What is meant by Hashtable is synchronized?

HashMap is non-synchronized. It is not thread-safe and can't be shared between many threads without proper synchronization code whereas Hashtable is synchronized. It is thread-safe and can be shared with many threads.

What does it mean that a Vector is synchronized?

Synchronization: Vector is synchronized, which means only one thread at a time can access the code, while ArrayList is not synchronized, which means multiple threads can work on ArrayList at the same time.

Is Hashtable class is synchronized?

Hashtable methods are synchronized, but that only provides method-level protection against race conditions. (So a Hashtable —unlike a HashMap —will not become internally corrupted if multiple threads are concurrently trying to modify the data.) It is only in this sense that Hashtable is thread-safe.

What does synchronized mean in Java?

Synchronization in java is the capability to control the access of multiple threads to any shared resource. In the Multithreading concept, multiple threads try to access the shared resources at a time to produce inconsistent results. The synchronization is necessary for reliable communication between threads.


Video Answer


1 Answers

Practically it means two things:

  1. Don't use them unless you will be sharing them between threads (if not just use HashMap or ArrayList).
  2. If you are sharing them between threads, check that the synchronization policies they implement actually are sufficient to make your program threadsafe (because the existence of some synchronization is little indication of the all-round concurrent behaviour of a class).
like image 140
jjujuma Avatar answered Oct 12 '22 03:10

jjujuma