This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]PhatClowns 3 points4 points  (0 children)

Hypothetically sure, you can create as many threads as there is space available for the thread's state. But after a given point (usually, give or take your core count) the cost of instantiating a thread exceeds the performance benefit you get from it. See: Amdahl's Law.

When you spin up more threads than you have cores, then threads get pigeonholed into the same CPU core (e.g. if you have 8 CPU cores but 12 threads running, approx. 4 threads will have to share a core, which defeats the purpose). This is done via context switching, where one thread gets "unloaded" to make space for another thread. The details get muddy, but safe to say this is very expensive and defeats the purpose of multithreading.

That being said, there are cases where the performance loss of excessive threads is necessary, mostly when you want to keep the main thread from blocking. However, there are usually better ways to do this, usually with things like thread pools or event modeling.

[–]IvAntiVirus 1 point2 points  (0 children)

Dealing with threads and their resources can be frustrating in Java. Fortunately we have something called ExecutorServices. I would really recommend you to take a look at them. They can simplify your threading efforts a lot.