[英]Iterator in Thread On CopyOnWriteArrayList not working
我正在为OCP考试做一些练习。 目前,我尝试在使用多个线程时打印出 CopyOnWriteArrayList的内容 。 根据文档,CopyOnWriteArrayList的迭代器将
打印列表中存在迭代器时存在的数字
因此,当开始在CopyOnWriteArrayList上写一个线程 ,并从CopyOnWriteArrayList 读另一个 线程时,我希望(有时)打印出一些项目。 在写完线程完成后遍历CopyOnWriteArrayList时,我希望新的迭代器打印出所有项目 。
不幸的是,这在使用Runnable接口的匿名实现时不起作用 。 但是,当使用自定义线程类时,它可以工作,在该类中,我传递对CopyOnWriteArrayList的引用。
现在,我不明白为什么会这样。 我不确定final CopyOnWriteArrayList<Integer> intList = new CopyOnWriteArrayList<>();
导致这种效果-如果是这样-我根本不知道为什么吗?
有关此主题的任何意见将不胜感激!
使用Runnable的匿名实现-不能按预期工作
static void copyOnWriteCollections() {
System.out.println("copyOnWriteCollections".toUpperCase());
final CopyOnWriteArrayList<Integer> intList = new CopyOnWriteArrayList<>();
Thread tADD = new Thread(new Runnable() {
public void run() {
for (int i = 0; i < 10000; i++) {
intList.add(i);
}
}
});
tADD.start();
/// will print the numbers in the list which are present
/// at the moment the iterator is created
Thread tREAD = new Thread(new Runnable() {
public void run() {
for (Integer i : intList) {
System.out.print(i);
System.out.print(" ");
}
}
});
tREAD.start();
try {
tADD.join();
tREAD.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
// will print out all numbers - as iterator is created when all
// modifications
// to intList have finished
System.out.println();
new Thread(new Runnable() {
public void run() {
System.out.println("new");
System.out.println("\tsize of intList: " + intList.size());
Iterator<Integer> it = intList.iterator();
while (it.hasNext()) {
int i = it.next();
System.out.print(i);
System.out.print(" ");
}
System.out.println("end");
}
}).start();
}
使用自定义线程类 - 按预期工作
class CopyOnWriteThread extends Thread {
Iterator<Integer> it = null;
boolean read = false;
CopyOnWriteArrayList<Integer> intList = null;
public CopyOnWriteThread(CopyOnWriteArrayList<Integer> intList, boolean read) {
this.intList = intList;
this.read = read;
}
public void run() {
if (read) {
it = intList.iterator();
System.out.println("START read (" + Thread.currentThread().getName() + ")");
System.out.print("\t");
while (it.hasNext()) {
System.out.print(it.next());
System.out.print(" ");
}
System.out.println();
System.out.println("END read (" + Thread.currentThread().getName() + ")");
} else {
System.out.println("START write (" + Thread.currentThread().getName() + ")");
for (int i = 0; i < 10000; i++) {
intList.add(i);
}
System.out.println("END write (" + Thread.currentThread().getName() + ")");
}
}
}
static void copyOnWriteCollections_CustomThread() {
CopyOnWriteArrayList<Integer> intList = new CopyOnWriteArrayList<>();
CopyOnWriteThread tWRITE = new CopyOnWriteThread(intList, false);
CopyOnWriteThread tREAD = new CopyOnWriteThread(intList, true);
tWRITE.start();
tREAD.start();
try {
tWRITE.join();
tREAD.join();
} catch (InterruptedException e) {
}
CopyOnWriteThread tREAD2 = new CopyOnWriteThread(intList, true);
tREAD2.start();
}
目前我正在使用JDK 1.7.0_25和Eclipse Mars Release 4.5.0的Win10机器
正如@ user2357112发布的,代码没有任何错误。 这个问题似乎与Eclipse有关 !
在Eclipse Mars / JRE 1.7.0_25中运行问题代码时,它不会打印出0到9999之间的数字。相反,它会立即退出,就好像没有新的迭代器一样 。
我从命令行编译代码并使用java.exe
和javaw.exe
启动它。 两者都表明输出正确。
Java命令
java -classpath "c:\Chapter14\ConcurrentCollections\bin" concurrentCollections.TestClass
结果
javaw命令
javaw -classpath "c:\Chapter14\ConcurrentCollections\bin" concurrentCollections.TestClass > "c:\test.txt"
结果
现在我可以说(肯定)我的代码和JVM没有任何问题!
编辑
我通过修改运行配置将输出重定向到文件。 在eclipse中重启了应用程序。 Output.txt
包含所有数字 - 但在我的Eclipse控制台窗口( 分配控制台 )中,数字不会出现...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.