• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread Performance Question

 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to have a task which occurs quite often but at irregular intervals are you better off keeping a single thread running or spawning a new thread to perform each task?
Does creating a new thread each time suck up more memory and processor than a single thread which runs for the life of the application?
How would you mesasure this?
Thank you,
Drew
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are going to have a task which occurs quite often but at irregular intervals are you better off keeping a single thread running or spawning a new thread to perform each task?
I would probably do it all with a single thread. Check out java.util.Timer, which has done most of the work for you.
Does creating a new thread each time suck up more memory and processor than a single thread which runs for the life of the application?
Probably yes. However the difference may well be too small to matter; it depends on how many tasks you're talking about, and how long they take, and what else is going on in the JVM and processor, and what's the phase of the moon... very difficult to give a solid general answer here.
How would you mesasure this?
Ideally with something like JProbe, if you / your employer can pay for it. But there's some good free tools as well. JAMon looks like a good candidate.

Do you have some working code that is executing too slowly? Are you interested in a general answer to this question? You can always
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of the spawned thread? parallel processing, or program responsivness?
 
Drew Lane
Ranch Hand
Posts: 296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by CL Gilbert:
What is the purpose of the spawned thread? parallel processing, or program responsivness?

The thread does some HTTP polling.
For example, downloads current stock quotes, images, news, etc. on demand.
Drew
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would say keep a dedicated thread. But it does not have to be running. It should be in wait() state till its told again to fetch its data.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!