# Is the scheduler's queue ordered?

**URL:** <https://discourse.julialang.org/t/is-the-schedulers-queue-ordered/133414>\
**Category:** General Usage\
**Tags:** question, async\
**Created:** [October 24, 2025, 10:20am UTC](https://discourse.julialang.org/t/is-the-schedulers-queue-ordered/133414 "2025-10-24T10:20:18Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![WalterMadelim](https://avatars.discourse-cdn.com/v4/letter/w/3e96dc/32.png) [@WalterMadelim](https://discourse.julialang.org/u/WalterMadelim)\
**Post date:** [October 24, 2025, 10:20am UTC](https://discourse.julialang.org/t/is-the-schedulers-queue-ordered/133414/1 "2025-10-24T10:20:18Z")

</div>

I spawn 5 tasks in sequence 1 to 5 (I assume the function `foreach` is strictly ordered).

If I start julia with 1 default thread, it is ordered:

```julia-auto
❯ julia --threads=1,1
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.12.1 (2025-10-17)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org release
|__/ |

julia> function busy_wait(s)
           t = time()
           while time() - t < s nothing end
       end;

julia> t = time(); foreach(j -> Threads.@spawn( (busy_wait(1);println("j = $j, th = $(Threads.threadid()), $(time()-t) sec")) ), 1:5);

julia> j = 1, th = 2, 1.059445858001709 sec
j = 2, th = 2, 2.0860068798065186 sec
j = 4, th = 2, 4.086081027984619 sec
j = 5, th = 2, 5.086113929748535 sec
j = 3, th = 2, 3.0860419273376465 sec

```

(But the print is orderless, why? I think it’s messy.)

If I start with 3 default threads, then the behavior appears to be random. Why?

```julia-auto
❯ julia --threads=3,1   
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.12.1 (2025-10-17)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org release
|__/ |

julia> function busy_wait(s)
           v = [1, 2, 3]
           t = time()
           while time() - t < s
               circshift!(v, 7)
           end
           return v
       end;

julia> function mytask(j, t0)
           enter_time = time() - t0
           busy_wait(1)
           finish_time = time() - t0
           j, enter_time, finish_time
       end;

julia> const t0 = time();

julia> tasks = map(j -> Threads.@spawn( mytask(j, t0) ), 1:5);

julia> ret = fetch.(tasks)
5-element Vector{Tuple{Int64, Float64, Float64}}:
 (1, 0.08308601379394531, 1.0830869674682617)
 (2, 0.08307099342346191, 1.083070993423462)
 (3, 1.0831928253173828, 2.083192825317383)
 (4, 0.08305788040161133, 1.0830588340759277) # Notice that the 4-th task cuts in
 (5, 1.0831959247589111, 2.083195924758911)

❯ julia --threads=3,1
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.12.1 (2025-10-17)
 _/ |\ __'_|_|_|\__'_| | Official https://julialang.org release
|__/ |

julia> function busy_wait(s)
           v = [1, 2, 3]
           t = time()
           while time() - t < s
               circshift!(v, 7)
           end
           return v
       end;

julia> function mytask(j, t0)
           enter_time = time() - t0
           busy_wait(1)
           finish_time = time() - t0
           j, enter_time, finish_time
       end;

julia> const t0 = time();

julia> tasks = map(j -> Threads.@spawn( mytask(j, t0) ), 1:5);

julia> ret = fetch.(tasks)
5-element Vector{Tuple{Int64, Float64, Float64}}:
 (1, 0.08526611328125, 1.0852670669555664)
 (2, 0.08526015281677246, 1.0852611064910889)
 (3, 0.08526611328125, 1.08526611328125) # in this case it is ordered
 (4, 1.0854401588439941, 2.085440158843994)
 (5, 1.0854201316833496, 2.0854201316833496)

```
