1
我做水木清华这样的:鉴于的Groovy /了SoapUI:环内螺纹不起作用
Iterator<String> iterator = requestList.iterator()
(1..threadCount).each {
Thread.start {
while(iterator.hasNext()) {
log.info iterator.next()
Thread.sleep(50)
}
}
}
是threadCount = 10
和requestList is ~115
,我希望所有的线程输出的所有列表中,每次迭代器要求给他们下一个。
不过,我几乎连得10个日志,通常8
代替log.info其实我打算触发与数N的REST请求一切了SoapUI Groovy脚本的步骤中完成的,
我在做什么这些线程有问题?
UPD
好吧,没水木清华笨的像这样,为了测试(和避免使用一个数组):
def array1 = all[0..5]
def array2 = all[6..11]
Thread.start{
for(String r: array1) {
log.info r
}
}
Thread.start{
for(String r: array2) {
log.info r
}
}
现在我根本没有输出或一个日志最多,虽然我期望12. 如何创建将同时输出数据的线程?
更
def threadCount=10
(0..threadCount).each { n ->
Thread.start {
(1..10).each {
log.info "thread"+n+" says| "+it
}
}
}
输出是:
thread0 says| 1
thread3 says| 1
thread8 says| 1
thread2 says| 1
thread1 says| 1
thread9 says| 1
thread7 says| 1
thread5 says| 1
thread4 says| 1
thread0 says| 2
仅此而已。再说一遍,我或者常规怎么了? (希望groovy很好)
这将导致竞争条件和数据竞争以及因为iterator对象是没有任何同步所有线程共享。所以输出是不可预测的。 – Madhusudhan
你偶然发现线程安全问题。相关的问题是这一个:http://stackoverflow.com/questions/5847939/is-list-iterator-thread-safe – Danilo
关于您的更新,正确定义数组以查看您所期望的输出:def array1 = 0。 0.5。在for循环中你应该使用Integer,但String也可以。 – Danilo