JAVA集合类测试、原理以及多线程测试比较
2013-10-24 11:41
246 查看
arrayList分析:
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
实现了泛型
RandomAccess是一个标记接口,接口内没有定义任何内容。
通过实现 java.io.Serializable 接口以启用其序列化功能。
private transient Object[] elementData;
private int size;
elementData:是元素,size为大小
transient:JAVA关键字,用transient声明的域,不会被Serializable序列化。
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
这个关键在于ensureCapacityInternal方法,方法代码如下,保证超过当前容量再进行扩容:
private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
============================================================================================================
多线程测试:
public class ArrayListTest{
public static void main(String args[]) throws InterruptedException {
//List<String> v = new ArrayList<String>();
List<String> v = (List<String>) Collections.synchronizedList(new ArrayList<String>());
Thread4Collection hello1 = new Thread4Collection("hello1", v);
Thread4Collection hello2 = new Thread4Collection("hello2", v);
Thread4Collection hello3 = new Thread4Collection("hello3", v);
Thread h1 = new Thread(hello1);
Thread h2 = new Thread(hello2);
Thread h3 = new Thread(hello3);
h1.start();
h2.start();
h3.start();
}
}
public class Thread4Collection extends Thread{
String name;
List<String> v;
int a = 5;
public Thread4Collection(String name, List<String> v) {
this.name = name;
this.v = v;
}
public void run() {
this.test();
}
public void test(){
System.out.println(name + "start");
while (a <10) {
v.add(name + ".add");
System.out.println(name + " list size is " + v.size());
System.out.println(name + " list is " + v);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
a++;
}
}
}
List<String> v = (List<String>) Collections.synchronizedList(new ArrayList<String>());
可以使arrayList实现同步,保证线程安全
System类arrayCopy方法测试
public static void main(String[] args) {
//System.console();
int[] ids = { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(ids));
//测试自我复制
//把从索引0开始的2个数字复制到索引为3的位置上
System.arraycopy(ids, 0, ids, 3, 2);
System.out.println(Arrays.toString(ids)); // [1, 2, 3, 1, 2]
//复制到另一个数组中
int[] ids2 = new int[ids.length];
System.arraycopy(ids, 0, ids2, 0, ids.length);
System.out.println(Arrays.toString(ids2));
}
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
实现了泛型
RandomAccess是一个标记接口,接口内没有定义任何内容。
通过实现 java.io.Serializable 接口以启用其序列化功能。
private transient Object[] elementData;
private int size;
elementData:是元素,size为大小
transient:JAVA关键字,用transient声明的域,不会被Serializable序列化。
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
这个关键在于ensureCapacityInternal方法,方法代码如下,保证超过当前容量再进行扩容:
private void ensureCapacityInternal(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
============================================================================================================
多线程测试:
public class ArrayListTest{
public static void main(String args[]) throws InterruptedException {
//List<String> v = new ArrayList<String>();
List<String> v = (List<String>) Collections.synchronizedList(new ArrayList<String>());
Thread4Collection hello1 = new Thread4Collection("hello1", v);
Thread4Collection hello2 = new Thread4Collection("hello2", v);
Thread4Collection hello3 = new Thread4Collection("hello3", v);
Thread h1 = new Thread(hello1);
Thread h2 = new Thread(hello2);
Thread h3 = new Thread(hello3);
h1.start();
h2.start();
h3.start();
}
}
public class Thread4Collection extends Thread{
String name;
List<String> v;
int a = 5;
public Thread4Collection(String name, List<String> v) {
this.name = name;
this.v = v;
}
public void run() {
this.test();
}
public void test(){
System.out.println(name + "start");
while (a <10) {
v.add(name + ".add");
System.out.println(name + " list size is " + v.size());
System.out.println(name + " list is " + v);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
a++;
}
}
}
List<String> v = (List<String>) Collections.synchronizedList(new ArrayList<String>());
可以使arrayList实现同步,保证线程安全
System类arrayCopy方法测试
public static void main(String[] args) {
//System.console();
int[] ids = { 1, 2, 3, 4, 5 };
System.out.println(Arrays.toString(ids));
//测试自我复制
//把从索引0开始的2个数字复制到索引为3的位置上
System.arraycopy(ids, 0, ids, 3, 2);
System.out.println(Arrays.toString(ids)); // [1, 2, 3, 1, 2]
//复制到另一个数组中
int[] ids2 = new int[ids.length];
System.arraycopy(ids, 0, ids2, 0, ids.length);
System.out.println(Arrays.toString(ids2));
}
相关文章推荐
- Remoting、Reflection以及本地调用的性能测试比较
- 弱网测试原理以及方法(一)
- HBase 高性能获取数据(多线程批量式解决办法) + MySQL和HBase性能测试比较
- opencv3UMat介绍,以及测试比较opencl,cuda性能
- 浅谈多线程编程以及锁的效率测试
- 『PyTorch』第五弹_深入理解Tensor对象_中下:数学计算以及numpy比较_&_广播原理简介
- Java多线程笔记二(synchronized的使用以及实现原理)
- 用 ConTest 进行多线程单元测试 - 为什么并行测试很困难以及如何使用 ConTest 辅助测试
- Spark中的广播变量原理以及测试(共享变量是spark中第二个抽象)
- coreData的原理和使用以及coreData中的多线程问题(二)
- redmine比较慢以及测试邮件发送完成收不到邮件
- cyclictest 测试以及原理简介
- LVS 三种工作模式原理、以及优缺点比较
- 使用 ConTest 进行多线程单元测试 - 为什么并行测试很困难以及如何使用 ConTest 辅助测试
- java +号字符串拼接原理解释以及StringBulider比较
- darkstar 线程互斥原理以及测试!
- 学会使用函数编程,以及设计比较完备的测试用例完成对小程序的测试
- python 多线程与多进程的简单测试比较
- Android多线程--HandlerThread的使用以及原理
- 常见加密算法分,用途,原理以及比较