Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a limit to how many threads I can make

Ok, so I've tagged this question with java because I'm primarily thinking about it as that is the language I'm writing it currently. However, this is equally as general in the programming world.

My question is: is there any limit to how many threads a processor can handle, and more over what is the likelihood of my application maxing this limit? AND what would happen if I did?

I don't really know much about threads - I just know that they enable you to execute two processes at once (that's probably not even the correct term). Initially, I thought single core processors didn't allow for multiple threads but then common sense kicked in and I thought about how that couldn't be possible given Windows and things. But now I'm wondering, what is the difference between single core and multi core processors / what do multi core processors allow?

Explanations in the simplest terms possible would be much appreciated. Thanks in advance

like image 728
Andy Avatar asked Dec 26 '22 20:12

Andy


2 Answers

A processor can normally run one thread at a time. It can handle far more than that, however. When you have several processes running, your processor(s) switches between them very quickly, giving each one a slice of the processor's time. This gives an illusion of concurrency, but really everything is happening in serial.

There is not really a hard limit on the number of threads you can run, but there is overhead involved when the processor switches between threads. If you're building an application with a GUI, it's a good idea to spawn a separate thread for long-running tasks so that the GUI can stay responsive. If you expect to run on a computer with multiple processors or some other solution which allows true concurrency, you can try to take advantage of that by running in separate threads, but it does make your problem more complex and can cause bugs which are hard to track down.

Most computers are multi-core today and can actually run several threads concurrently. In fact more concurrency, rather than stronger individual processors, is likely how computers will develop in the future. Being able to take advantage of that is important, but it's hard. If you don't have a specific reason to make your application multithreaded, I recommend avoiding it until you feel comfortable doing so.

like image 167
Jacob Raihle Avatar answered Jan 10 '23 22:01

Jacob Raihle


In Java you will hit a limit creating threads when you run out of memory to process them. It took me about 10,000 on 2GiB of RAM to do this. Once you do hit an OutOfMemoryException you will have problems unless you already have a shell/Task manager open as you may strugle to kill the JVM process.

In terms of processors, they only run 1 (or 2) per core at a time. It is the job of the scheduler (part of your OS) to allocate time so thread A will run for a little bit, then thread 2 etc. In this way they allow you to do multiple things at a time. If you have a multi-core cpu, the scheduler will also take charge of allocating which thread goes to which core, allowing you to take advantage of this with no extra effort.

In general, you don't need to worry too much about this. Just create multiple threads when you need things to be processed simultaneously (for example, GUI code has to keep processing while background tasks happen). Most appliations have <20 threads anyway.

like image 22
James Avatar answered Jan 10 '23 21:01

James