Grails 2.4无法迭代列表,设置或映射。虽然代码在Java中运行良好。
public void createData(){
Set<String> entries = new HashSet<String>();
entries.add("a");
entries.add("b");
entries.add("c");
ThreadGroup tg = new ThreadGroup("Async Threads");
for(final String entry : entries){
new Thread(tg, "xyz"){
public void run(){
System.out.println(entry);
}
}.start();
}
}
使用Java 7在Grails 2.4中输出
a
b
b
Java 7中的输出
a
b
c
可能是什么原因?
答案 0 :(得分:0)
看一下:For loop works different in Groovy and Java
尝试这样做:
Set<String> entries = new HashSet<String>();
entries.add('a');
entries.add('b');
entries.add('c');
final ThreadGroup tg = new ThreadGroup('Async Threads');
for (final String entry : entries) {
new Thread(tg, entry) {
void run() {
println(name)
}
}.start();
}
你会看到groovy代码工作正常