If you want to save tasks from the for loop, you need to allocate an array and push the tasks into it, e.g.:
using ChunkSplitters
array1 = repeat([1,2],1111)
chk = chunks(1:length(array1); n=Threads.nthreads(), split=:batch)
counts_total = Dict(ii => 0 for ii in [1,2])
tasks = Task[]
for inds in chk
task = Threads.@spawn begin
sub_counts = Dict(ii => 0 for ii in [1,2])
for val in array1[inds]
sub_counts[val] += 1
end
return sub_counts
end
push!(tasks, task)
end
thread_sums = fetch.(tasks)
for sub in thread_sums
merge!(+,counts_total,sub)
end
counts_total
I would generally recommend using OhMyThreads.jl though