线程安全的类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<>());
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix