# Strange behavior of @spawn

**URL:** <https://discourse.julialang.org/t/strange-behavior-of-spawn/43442>\
**Category:** New to Julia\
**Created:** [July 21, 2020, 6:44pm UTC](https://discourse.julialang.org/t/strange-behavior-of-spawn/43442 "2020-07-21T18:44:23Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 21, 2020, 6:44pm UTC](https://discourse.julialang.org/t/strange-behavior-of-spawn/43442/1 "2020-07-21T18:44:23Z")

</div>

I do not understand what is going on here:

```julia
julia> test(i,j) = println("i = $i , j = $j")
       i = 0 
       for j in 1:4
         global i += 1
         Threads.@spawn test(i,j)
       end

i = 4 , j = 2
julia> i = 4 , j = 1
julia> i = 4 , j = 3
i = 4 , j = 4

```

Why I get the same `i=4` value for all calls?

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [July 21, 2020, 6:48pm UTC](https://discourse.julialang.org/t/strange-behavior-of-spawn/43442/2 "2020-07-21T18:48:11Z")

</div>

Well, I think that the function takes longer to be called in the specific thread than what the loop takes to run… correct?

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [July 21, 2020, 6:49pm UTC](https://discourse.julialang.org/t/strange-behavior-of-spawn/43442/3 "2020-07-21T18:49:42Z")

</div>

Yes. It is exactly that. You can see the difference if you put a `sleep(1)` after each `@spawn`.
