标签:arraylist nbsp ati www cap app mode width outline
voctor动态数组、同步类容器,底层实现基于:Collections.synchronized
package demo5;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Vector;
/**
* Created by liudan on 2017/7/9.
*/
public class MyThread2 extends Thread {
/*同步类容器、动态数组*/
public static void main(String[] args) {
/*final Vector<String> ticksts = new Vector<>();*/
List<String> ticksts = Collections.synchronizedList(new ArrayList<String>());
for (int i = 1; i <= 100; i++) {
ticksts.add("G1001-火车票-G000X" + i);
}
for (int i = 1; i <= 10; i++) {
new Thread("线程" + i) {
public void run() {
while (true) {
if (ticksts.isEmpty()) break;
System.err.println(Thread.currentThread().getName() + "\t" + ticksts.remove(0));
}
}
}.start();
}
}
}
输出:
线程001 G1001-火车票-G000X1
线程001 G1001-火车票-G000X2
线程001 G1001-火车票-G000X3
线程001 G1001-火车票-G000X4
线程002 G1001-火车票-G000X5
线程002 G1001-火车票-G000X7
线程001 G1001-火车票-G000X6
线程001 G1001-火车票-G000X9
线程001 G1001-火车票-G000X10
线程002 G1001-火车票-G000X8
16.同步类容器Collections.synchronized
标签:arraylist nbsp ati www cap app mode width outline
原文地址:http://www.cnblogs.com/xxt19970908/p/7302392.html