Skip to content

Instantly share code, notes, and snippets.

@SansWord
Last active February 17, 2017 03:49
Show Gist options
  • Save SansWord/c120fc82831f985bb432b4388995c2c9 to your computer and use it in GitHub Desktop.
Save SansWord/c120fc82831f985bb432b4388995c2c9 to your computer and use it in GitHub Desktop.
======= expection results for 10 elements, and every values are 1 =======
======= test for 1000 times =========
Exception in thread "Thread-4" java.lang.ArrayIndexOutOfBoundsException: 1
at java.util.ArrayList.add(ArrayList.java:459)
at java_util_List$add.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at ThreadSafeTest$_parallelExecute_closure1$_closure6.doCall(ThreadSafeTest.groovy:6)
at ThreadSafeTest$_parallelExecute_closure1$_closure6.doCall(ThreadSafeTest.groovy)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
at org.codehaus.groovy.runtime.metaclass.ClosureMetaClass.invokeMethod(ClosureMetaClass.java:294)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
at groovy.lang.Closure.call(Closure.java:414)
at groovy.lang.Closure.call(Closure.java:408)
at groovy.lang.Closure.run(Closure.java:495)
at java.lang.Thread.run(Thread.java:745)
====== error =====
results: [null, null, 1, 1, 1, 1, 1, 1, 1, 1], size: 10
====== error =====
happened at 0
====== error =====
results: [1, 1, 1, 1, null, 1, 1, 1, 1, 1], size: 10
====== error =====
happened at 375
====== error =====
results: [1, 1, 1, 1, null, 1, 1, 1, 1, 1], size: 10
====== error =====
happened at 490
====== error =====
results: [1, 1, 1, 1, 1, 1, null, 1, 1, 1], size: 10
====== error =====
happened at 805
======= success: 996 / 1000
def parallelExecute(List<Closure> executions) {
def results = []
def threads = []
executions.collect { execution ->
def th = new Thread({
results.add(execution())
})
threads.add(th)
}
threads.each { it.start() }
threads.each { it.join() }
results
}
def singleTest(listValue, execNum) {
def execs = []
for(int i =0; i<execNum; i++) {
execs.add({ -> listValue})
}
def results = parallelExecute(execs);
def size = results.size
if( !size == execNum || !results.every { elm -> elm == listValue}) {
println "====== error ====="
println "results: $results, size: $size"
println "====== error ====="
return false
} else {
return true
}
}
println "======= expection results for 10 elements, and every values are 1 ======="
def testNum = 1000
println "======= test for $testNum times ========="
def success = 0
for(int i = 0; i < testNum; i++) {
if(!singleTest(1, 10)) {
println "happened at $i"
} else {
success++
}
}
println "======= success: $success / $testNum"
def parallelExecute(List<Closure> executions) {
def results = []
def threads = []
executions.collect { execution ->
def resultContainer = []
results.add(resultContainer)
def th = new Thread({
resultContainer.add(execution())
})
threads.add(th)
}
threads.each { it.start() }
threads.each { it.join() }
results.collect { resultContainer -> resultContainer[0] }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment