
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to make a collection thread safe in java?
The Collections class of java.util package methods that exclusively work on collections these methods provide various additional operations which involves polymorphic algorithms.
This class provides different variants of the synchronizedCollection() method as shown below −
Sr.No | Methods & Description |
---|---|
1 | static <T> Collection<T> synchronizedCollection(Collection<T> c) This method accepts any collection object and, returns a synchronized (thread-safe) collection backed by the specified collection. |
2 | static <T> List<T> synchronizedList(List<T> list) This method accepts an object of the List interfacereturns a synchronized (thread-safe) list backed by the specified list. |
3 | static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) This method accepts an object of the Map interface and, returns a synchronized (thread-safe) map backed by the specified map. |
4 | static <T> Set<T> synchronizedSet(Set<T> s) This method accepts an object of Set interface and, returns a synchronized (thread-safe) set backed by the specified set. |
5 | static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) This method accepts an object of the Map interface and, returns a synchronized (thread-safe) sorted map backed by the specified sorted map. |
6 | static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) This method accepts an object of the synchronizedSortedSet interface and, returns a synchronized (thread-safe) sorted set backed by the specified sorted set. |
Example
import java.util.Collection; import java.util.Collections; import java.util.Vector; public class CollectionReadOnly { public static void main(String[] args) { //Instantiating an ArrayList object Vector<String> vector = new Vector<String>(); vector.add("JavaFx"); vector.add("Java"); vector.add("WebGL"); vector.add("OpenCV"); System.out.println(vector); Collection<String> synchronizedVector = Collections.synchronizedCollection(vector); System.out.println("Synchronized "+synchronizedVector); synchronizedVector.add("CoffeeScript"); } }
Output
[JavaFx, Java, WebGL, OpenCV] Synchronized [JavaFx, Java, WebGL, OpenCV]
- Related Articles
- How to make a class thread-safe in Java?
- Which collection classes are thread-safe in Java?
- Thread Safe Concurrent Collection in C#
- How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?\n
- Make your collections thread-safe in C#
- Is Java matcher thread safe in Java?
- Is Swing thread-safe in Java?
- How to make a collection read only in java?
- Thread-Safe collections in C#
- How to make an object eligible for garbage collection in Java?
- How to create a thread in Java
- How a thread can interrupt another thread in Java?
- Do you think a Python dictionary is thread safe?
- How to interrupt a running thread in Java?
- Check if ArrayList is Synchronized (thread safe) in C#

Advertisements