File tree 1 file changed +14
-6
lines changed
kotlinx-coroutines-core/src/test/kotlin/guide/test
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>()
32
32
fun ignoreLostThreads (vararg s : String ) { ignoreLostThreads + = s }
33
33
34
34
fun 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
+ }
41
49
}
42
50
43
51
fun checkTestThreads (threadNamesBefore : Set <String >) {
You can’t perform that action at this time.
0 commit comments