'the execution result of kotlin coroutine not excepted

 @Test
fun test():Unit= runBlocking(){
    var count =0
    List(1000){
      GlobalScope.launch() {
          println("${Thread.currentThread().name}       $count")
          count++
      }
    }.joinAll()
    println(count)
  }

I except it output less than 1000,but it always output 1000.I tried many times,even change count 10000,it alwaya output 1000 or 10000.When i read the log,it seems to be a thread synchronization problem, but the end result is correct again.Log like this

DefaultDispatcher-worker-7 @coroutine#8 0
DefaultDispatcher-worker-9 @coroutine#10 0
DefaultDispatcher-worker-6 @coroutine#7 0
DefaultDispatcher-worker-4 @coroutine#5 0
DefaultDispatcher-worker-2 @coroutine#3 0
DefaultDispatcher-worker-1 @coroutine#2 0
DefaultDispatcher-worker-8 @coroutine#9 0
DefaultDispatcher-worker-11 @coroutine#12 6
DefaultDispatcher-worker-5 @coroutine#6 0
DefaultDispatcher-worker-3 @coroutine#4 0
DefaultDispatcher-worker-10 @coroutine#11 4
DefaultDispatcher-worker-12 @coroutine#13 10
DefaultDispatcher-worker-8 @coroutine#14 12
DefaultDispatcher-worker-10 @coroutine#15 12
at the end like this DefaultDispatcher-worker-8 @coroutine#908 899
DefaultDispatcher-worker-9 @coroutine#982 984
DefaultDispatcher-worker-9 @coroutine#998 985
DefaultDispatcher-worker-9 @coroutine#999 986
DefaultDispatcher-worker-9 @coroutine#1000 987
DefaultDispatcher-worker-9 @coroutine#1001 988
DefaultDispatcher-worker-8 @coroutine#997 989
DefaultDispatcher-worker-10 @coroutine#996 990
DefaultDispatcher-worker-1 @coroutine#995 991
DefaultDispatcher-worker-7 @coroutine#994 992
DefaultDispatcher-worker-4 @coroutine#993 993
DefaultDispatcher-worker-5 @coroutine#992 994
DefaultDispatcher-worker-2 @coroutine#991 995
DefaultDispatcher-worker-11 @coroutine#990 996
DefaultDispatcher-worker-6 @coroutine#909 899
DefaultDispatcher-worker-12 @coroutine#907 899
DefaultDispatcher-worker-3 @coroutine#906 899
1000 Can someone explain why this is



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source