Jump to content

  • Log In with Google      Sign In   
  • Create Account

Question on Threads


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 TheCreeperLawyer   Members   -  Reputation: 117

Like
0Likes
Like

Posted 22 September 2011 - 05:30 AM

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

Sponsor:

#2 Viscis   Members   -  Reputation: 102

Like
0Likes
Like

Posted 22 September 2011 - 06:01 AM

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.

#3 SiS-Shadowman   Members   -  Reputation: 359

Like
0Likes
Like

Posted 22 September 2011 - 06:24 AM

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).

#4 rip-off   Moderators   -  Reputation: 8726

Like
0Likes
Like

Posted 22 September 2011 - 08:36 AM

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.

#5 Viscis   Members   -  Reputation: 102

Like
0Likes
Like

Posted 22 September 2011 - 08:50 AM

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.

#6 Serapth   Crossbones+   -  Reputation: 5755

Like
0Likes
Like

Posted 22 September 2011 - 09:58 AM

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.

#7 phantom   Moderators   -  Reputation: 7563

Like
0Likes
Like

Posted 22 September 2011 - 10:13 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS