# Combining @sync and @async with for and while loops

**URL:** <https://discourse.julialang.org/t/combining-sync-and-async-with-for-and-while-loops/79424>\
**Category:** General Usage\
**Tags:** question\
**Created:** [April 13, 2022, 9:44am UTC](https://discourse.julialang.org/t/combining-sync-and-async-with-for-and-while-loops/79424 "2022-04-13T09:44:33Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tine](https://avatars.discourse-cdn.com/v4/letter/t/9de053/32.png) [@Tine](https://discourse.julialang.org/u/Tine)\
**Post date:** [April 13, 2022, 9:44am UTC](https://discourse.julialang.org/t/combining-sync-and-async-with-for-and-while-loops/79424/1 "2022-04-13T09:44:33Z")

</div>

I am relatively new to Julia, and I am trying to understand the @sync and @async macro, and how they act when combined with for and while loops.

I wonder if someone could tell me what the difference between these two are, if any?

```julia
function do_calculations(e)
    # Simulate time it takes to do some calculation
    sleep(e.time_to_do_calculations)
end

# Alternative 1
@sync begin
    for e in lst
        @async begin
            while running
                do_calculations(e)
            end
        end
    end
end

# Alternative 2
@sync for e in lst
    @async while running
        do_calculations(e)
    end
end

```

in `lst` there is elements that we want to do calculations on using `do_calculations(e)`, where each calculation takes some time. However they take different times depending on the element `e`. But I want each of the calculations on the different elements to run independently from each other, thus the use of `@async`. `running` is used to define how long these calculations should be running for.

Thanks in advance!
