# Why "Threads.@threads for " doesn't use all possible threads?

**URL:** https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711
**Category:** New to Julia
**Tags:** multithreading
**Created:** [January 23, 2020, 7:07pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711 "2020-01-23T19:07:40Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [January 23, 2020, 7:07pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711/1 "2020-01-23T19:07:40Z")

</div>

Good day.  
I have 12 cores. So nthread() gives 12.  
when I used "Threads.@threads for ", only 8 thread was created.  
RAM memory was utilized for up to 85%.

Can memory limitations and the number of thread created be connected?

thanks.

---

<div class="post-metadata">

### Author: ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)
#### Post date: [January 23, 2020, 8:41pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711/2 "2020-01-23T20:41:00Z")

</div>

Please show what code you ran.

---

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [January 23, 2020, 8:45pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711/3 "2020-01-23T20:45:02Z")

</div>

```julia
 function main()
     files=readdir()
     Threads.@threads for file in files
            if file[end-1:end]=="gz"
             println("threadid-",threadid(), " ", file)
             df=ParseFile(file)
             println("threadid-",threadid(), " ","Parsefile")
             df=KeepUniqueId(df)
             println("threadid-",threadid(), " ","KeepUnique")
             dfNews=ParseSubjects(df)
             println("threadid-",threadid(), " ","ParseSubjests")
             filename=file[1:end-7]
             CSV.write("$file.csv",dfNews)
         end
     end
end

```

The DataFrame size is around 1Gb.

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [January 23, 2020, 8:52pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711/4 "2020-01-23T20:52:34Z")

</div>

See the first item here: [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

### Author: ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)
#### Post date: [January 23, 2020, 9:08pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711/5 "2020-01-23T21:08:35Z")

</div>

> [@BMval](#):
>
> files=readdir() Threads.@threads for file in files

How many files are in that directory?

---

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [January 23, 2020, 9:09pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711/6 "2020-01-23T21:09:52Z")

</div>

Around 10-12. it’s more than a number of threads.

---

<div class="post-metadata">

### Author: ![BMval](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bmval/32/7647_2.png) [@BMval](https://discourse.julialang.org/u/BMval)
#### Post date: [January 23, 2020, 9:38pm UTC](https://discourse.julialang.org/t/why-threads-threads-for-doesnt-use-all-possible-threads/33711/7 "2020-01-23T21:38:26Z")

</div>

Maybe I got the answer  
“Threads.@threads for” tries distribute evenly a number of threads since when I have exactly 12 files, 12 threads were created.
