Write to text file while using threads

I think finally found the solution to my problem.
I used the following code in order to write to file correctly while still being able to use multithreading:

using Base.Threads: @threads, @spawn, @sync
using DelimitedFiles

a = zeros(10,5)

for j in 1:5
    @sync for i in 1:10
       @spawn begin
            a[i,j] = sum(rand(5000,5000)*rand(5000,5000))
       end
    end
    writedlm("test_mt_save.txt", a[:,1:j])
end

This way for each value of i, a new task will be spawned. But putting @sync in front of the inner loop, I am sure the tasks finish before writing to file.
It seems to work.
Please correct me if I wrote something which is not correct.

Thanks again,
Olivier

Edit: I edited the code following @StevenWhitaker 's comment about sum(sum(matrix)) = sum(matrix)