This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

hundreds of threads  RSS feed

 
Sev Zaslavsky
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm considering writing an application that will potentially have hundreds of threads instantiated, although only a handful of them will be runnable/running at any moment.

Can anyone comment on the performance impact of having many many threads that are sleeping?
 
Jules Bach
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I only ever used a threads sleep method when I was trying to simulate processing or work of some kind going on in my app, or to be sure on the order of threads reaching a critical section when tracing or debugging.

I'm not sure if the "sleep" method uses much CPU resources, or whether it is some kind of busywaiting for the time to expire.

If you get you threads to wait , and wake them upon a notify - then the only resource they'll use while blocked is memory, and your CPU won't waste any time on them while in this state.

That's they way i thought of it anyway, just chucking it out there...don't know if that's right...
[ December 09, 2008: Message edited by: Jules Bach ]
 
Angel Taveras
Ranch Hand
Posts: 84
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how much memory a single thread uses but you now that a thread have it's own call stack the bigger the more memory it will use. Another thing to consider it's that when sleeping the thread also keeps the locks.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
although only a handful of them will be runnable/running at any moment.


You might want to take a look at Thread pools instead of having 100 threads running. Google around for thread pools and you will get alot of help. If you are using java 5 there are alot of classes to take a look at in the java.util.concurrent package
 
Nitesh Kant
Bartender
Posts: 1638
IntelliJ IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sev: Can anyone comment on the performance impact of having many many threads that are sleeping?


Well do you really mean "sleeping" or "waiting". There is a whole lot of difference between these two.
Sleeping threads are like schedulers that wakes up after sometime even though it has nothing to do. Having a lot of scheduler threads are probably not a good idea as whenever a thread comes back to runnable state, the system has to do something called a context switch to put one of the running thread (of course if there is no processor available to run the woken up thread) out of work and giving the woken up thread a processor to run. This "may" affect performance and if this scheduler thread wakes up just to find there is nothing to do, it will be unnecessary.
On the other hand, "waiting" threads are kind of better because when they wake up, potentially, there will be some work to do and hence whatever is the impact is justified.

So, essentially you do not have to worry about the threads when they are sleeping but worry about them when they wake up!

If you want any detailed discussion on performance then you can request the mod of this forum to move it for you to the performance forum. But you would have to give more details about what your threads do and why do you need 100 threads?

Also, as Mathias has pointed out, it may be beneficial for you to look into Threadpools. They manage the number of threads by terminating the threads if not required.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've just fixed an applet that leaked up to 1,600+ threads on windows I was impressed it was alive (fixed now). I believe from reading up previously thread memory usage is configurable and dependent on jvm.

I've had a server with 100 (pooled) threads running very efficient / very fast (they actually got me to slow it down), this was using old school IO (no such thing should be necessary now with new IO) with a 100 odd connections.

So I suspect you can do it on whatever OS you have in mind but as said previously obviously pool them and you must be doing something very wacky to require 100s of threads.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!