
- 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
Which collection classes are thread-safe in Java?
A thread-safe class is a class that guarantees the internal state of the class as well as returned values from methods, are correct while invoked concurrently from multiple threads. The collection classes that are thread-safe in Java are Stack, Vector, Properties, Hashtable, etc.
Stack
The Stack class in Java implements the stack data structure that is based on the principle of LIFO. So, the Stack class can support many operations such as push, pop, peek, search, empty, etc.
Example
import java.util.*; public class StackTest { public static void main (String[] args) { Stack<Integer> stack = new Stack<Integer>(); stack.push(5); stack.push(7); stack.push(9); Integer num1 = (Integer)stack.pop(); System.out.println("The element popped is: " + num1); Integer num2 = (Integer)stack.peek(); System.out.println(" The element on stack top is: " + num2); } }
Output
The element popped is: 9 The element on stack top is: 7
Vector
An array of objects that grow as required is implemented by the Vector class in Java. The Vector class can support the methods like add(), remove(), get(), elementAt(), size(), etc
Example
import java.util.*; public class VectorTest { public static void main(String[] arg) { Vector vector = new Vector(); vector.add(9); vector.add(3); vector.add("ABC"); vector.add(1); vector.add("DEF"); System.out.println("The vector is: " + vector); vector.remove(1); System.out.println("The vector after an element is removed is: " + vector); } }
Output
The vector is: [9, 3, ABC, 1, DEF] The vector after an element is removed is: [9, ABC, 1, DEF]
- Related Articles
- How to make a collection thread safe in java?
- Thread Safe Concurrent Collection in C#
- Is Java matcher thread safe in Java?
- Is Swing thread-safe in Java?
- How to understand StringBuffer is thread-safe and StringBuilder is non-thread-safe in Java?\n
- How to make a class thread-safe in Java?
- What are collection classes in C#?
- Thread-Safe collections in C#
- Make your collections thread-safe in C#
- Check if ArrayList is Synchronized (thread safe) in C#
- What are the ways in which a thread is created in Java?
- What are Java classes?
- What are final classes in Java?
- What are abstract classes in Java?
- What are inner classes in Java?

Advertisements