File tree Expand file tree Collapse file tree 1 file changed +14
-6
lines changed
kotlinx-coroutines-core/src/test/kotlin/guide/test Expand file tree Collapse file tree 1 file changed +14
-6
lines changed Original file line number Diff line number Diff line change @@ -32,12 +32,20 @@ private val ignoreLostThreads = mutableSetOf<String>()
3232fun ignoreLostThreads (vararg s : String ) { ignoreLostThreads + = s }
3333
3434fun threadNames (): Set <String > {
35- val arrayOfThreads = Array <Thread ?>(Thread .activeCount()) { null }
36- val n = Thread .enumerate(arrayOfThreads)
37- val names = hashSetOf<String >()
38- for (i in 0 until n)
39- names.add(arrayOfThreads[i]!! .name)
40- return names
35+ var estimate = 0
36+ while (true ) {
37+ estimate = estimate.coerceAtLeast(Thread .activeCount() + 1 )
38+ val arrayOfThreads = Array <Thread ?>(estimate) { null }
39+ val n = Thread .enumerate(arrayOfThreads)
40+ if (n >= estimate) {
41+ estimate = n + 1
42+ continue // retry with a better size estimate
43+ }
44+ val names = hashSetOf<String >()
45+ for (i in 0 until n)
46+ names.add(arrayOfThreads[i]!! .name)
47+ return names
48+ }
4149}
4250
4351fun checkTestThreads (threadNamesBefore : Set <String >) {
You can’t perform that action at this time.
0 commit comments