1. 程式人生 > 其它 >執行緒安全的類StringBuffer、Hashtable、Vector

執行緒安全的類StringBuffer、Hashtable、Vector

 

 

 

package Thread.synchronizedDemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Vector;

/**
* 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<>();//執行緒不安全的


}
}