Question on Threads

Started by
5 comments, last by _the_phantom_ 12 years, 7 months ago
I have a quick question about software and hardware threads.

Can I have more software Threads than Hardware Threads.

E.g. I have a 12 thread CPU. If I write a program in Java with say 16 sub-classes that extend Thread and they all run simultaneously. Will that work or will it not have enough hardware threads.

Thanks
Advertisement

I have a quick question about software and hardware threads.

Can I have more software Threads than Hardware Threads.

E.g. I have a 12 thread CPU. If I write a program in Java with say 16 sub-classes that extend Thread and they all run simultaneously. Will that work or will it not have enough hardware threads.

Thanks


I'm not amazing with threads but as far as I'm aware, yes. Now somebody else back me up with the techie bit :)

Threads are concurrent executions of code from your program. So they are like separate programs? The hardware process can handle hundreds of programs running at once, each with their own threads.

Check Task Manager... (Click view at the top, select columns and tick threads). My Java is running 30 at the moment.
Yes it will work. Most operating systems will use some sort of scheduling in order to let each thread run a little, before it will be swapped for another one. For example, my computer at work has about 1150 threads running (with just a view actually doing stuff).
It will work, but you generally don't want more "busy" threads than you have cores. If you do, they'll fight each other for processor time. When designing a threaded application, it is good to keep this in mind.

It will work, but you generally don't want more "busy" threads than you have cores. If you do, they'll fight each other for processor time. When designing a threaded application, it is good to keep this in mind.



That's very interesting, I may have to rework how I do some things.

I have a quick question about software and hardware threads.

Can I have more software Threads than Hardware Threads.

E.g. I have a 12 thread CPU. If I write a program in Java with say 16 sub-classes that extend Thread and they all run simultaneously. Will that work or will it not have enough hardware threads.

Thanks


Keep in mind "threads" and "hardware threads" are two very different things. When Java issues a thread, it is not a hardware thread. There are literally thousands of threads in a system. It's only at the kernal or device driver level that you actually deal directly with hardware threads, otherwise it is absolutely virtualized.
If you have more 'active'/'busy' threads than cores the OS's schedular will given them each time, in turn, depending on priorities and other system metrics, for a set time period before they are interrupted and put into a queue.

So, if you have a single core, 6 threads with the same priority and a time quantum (aka period of time the thread gets to run before being swapped out) of 1ms then each thread would get 1ms of time every 6ms. Two cores mean you'd get a go every 3ms and so on.

However only 'active' threads are considered for activation; many threads in your OS right now are 'sleeping' thus won't be allocated runtime. Threads can be put to sleep for many reasons; waiting on events is an example, or the Sleep() function which most threading APIs support.

As noted if you have more 'active' threads in your application then you have cores to service them then the threads will get in each others way; a common solution these days is to not think of things at the 'thread' level and instead use a 'thread pool' to service short running 'tasks' which perform the work. The thread pool would create a fixed number of threads and then as work becomes avalible a thread takes it and works on it, then when done goes and grabs another piece and so on.

This topic is closed to new replies.

Advertisement