67,538
社区成员
发帖
与我相关
我的任务
分享
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Test {
public static void main(String[] args) {
int length = 5; // 假设5个就满
List<Long> list = new ArrayList<Long>(length);
thread1(list,length);
thread2(list,length);
}
public static void thread1(final List<Long> list, final int length) {
new Thread() {
public void run() {
while (list.size() < length) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list.add(System.currentTimeMillis()); // 每隔一秒存一个时间,直到存满为止
System.out.println("放入一个元素");
}
}
}.start();
}
public static void thread2(final List<Long> list, final int length) {
new Thread() {
public void run() {
boolean over = true;
while (over) {
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (list.size() >= length) {
System.out.println("list满,清空开始");
for(int i=0; i<list.size(); i++){
System.out.println(list.get(i));
}
list.removeAll(list);
System.out.println("清空list,list大小为:"+list.size());
over = false;
}
else{
System.out.println("没有满");
}
}
}
}.start();
}
}