线程安全的类StringBuffer、Hashtable、Vector

package Thread.synchronizedDemo;

import java.util.*;

/**
* FileName: synchronizedDemo01
* Author: lps
* Date: 2022/3/31 14:04
* Sign:刘品水 Q:1944900433
*/
public class synchronizedDemo01 {
public static void main(String[] args) {
StringBuffer stringBuffer = new StringBuffer();
// @Override
// public synchronized int length() {
// return count;
// }
//
// @Override
// public synchronized int capacity() {
// return value.length;
// }
//
//
// @Override
// public synchronized void ensureCapacity(int minimumCapacity) {
// super.ensureCapacity(minimumCapacity);
// }
//
// /**
// * @since 1.5
// */
// @Override
// public synchronized void trimToSize() {
// super.trimToSize();
// }
StringBuilder stringBuilder = new StringBuilder();//线程不安全

Vector<Object> objects = new Vector<>();
//public synchronized void copyInto(Object[] anArray) {
// System.arraycopy(elementData, 0, anArray, 0, elementCount);
// }
//
// /**
// * Trims the capacity of this vector to be the vector's current
// * size. If the capacity of this vector is larger than its current
// * size, then the capacity is changed to equal the size by replacing
// * its internal data array, kept in the field {@code elementData},
// * with a smaller one. An application can use this operation to
// * minimize the storage of a vector.
// */
// public synchronized void trimToSize() {
// modCount++;
// int oldCapacity = elementData.length;
// if (elementCount < oldCapacity) {
// elementData = Arrays.copyOf(elementData, elementCount);
// }
// }
ArrayList<Object> objects1 = new ArrayList<>();//线程不安全的

Hashtable<Object, Object> objectObjectHashtable = new Hashtable<>();
// public synchronized int size() {
// return count;
// }
//
// /**
// * Tests if this hashtable maps no keys to values.
// *
// * @return <code>true</code> if this hashtable maps no keys to values;
// * <code>false</code> otherwise.
// */
// public synchronized boolean isEmpty() {
// return count == 0;
// }
//
// /**
// * Returns an enumeration of the keys in this hashtable.
// *
// * @return an enumeration of the keys in this hashtable.
// * @see Enumeration
// * @see #elements()
// * @see #keySet()
// * @see Map
// */
// public synchronized Enumeration<K> keys() {
// return this.<K>getEnumeration(KEYS);
// }
//
// /**
// * Returns an enumeration of the values in this hashtable.
// * Use the Enumeration methods on the returned object to fetch the elements
// * sequentially.
// *
// * @return an enumeration of the values in this hashtable.
// * @see java.util.Enumeration
// * @see #keys()
// * @see #values()
// * @see Map
// */
// public synchronized Enumeration<V> elements() {
// return this.<V>getEnumeration(VALUES);
// }
HashMap<Object, Object> objectObjectHashMap = new HashMap<>();//线程不安全的

//static <T> List<T> synchronizedList(List<T> list)
//返回由指定列表支持的同步(线程安全)列表。
List<Object> list = Collections.synchronizedList(new ArrayList<>());

//static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
//返回由指定的Map支持的同步(线程安全)Map。
Map<Object, Object> objectObjectMap = Collections.synchronizedMap(new HashMap<>());

//static <T> Set<T> synchronizedSet(Set<T> s)
//返回一个由指定集合支持的同步(线程安全)集。
Set<Object> objects2 = Collections.synchronizedSet(new HashSet<>());


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值