Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Correct usage of threads ?  RSS feed

 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
Currently have about 5 applications that we support
Each application has a couple of schedulers that need to be monitored
Scheduler similar to Java Timer component

We now need to ensure that all these schedulers write to a commopn database

We were thinking of making this call through a thread

Main thread will spawn a new thread
Are there any risks in this approach ?
Would there be any memory issues ?
Would the spawned thread be taken care by the JVM

Regards,
-anagha
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, moving a time-consuming operation like writing to a database out of your main thread will get the response back to the user more quickly. This may be a few thousandths of a second, so profile the app to make sure it's worth the trouble.

Are all the applications in the same JVM? I'm not sure that's going to matter, but it's interesting.

Are they in a servlet container or EJB container or something else? EJB and maybe servlet specs tell you not to spawn your own threads, but most containers have some custom, not quite standard way to do it.
 
anagha patankar
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stan for the response .

All applications are in the same JVM

We are using a EJB container
I was a little worried regarding - if I spawn a new thread ( non daemon ) - will the JVM ensure that the thread is recycled .

Asking since : the Java timer is scheduled to run every two minutes

So in a day this scheduler will run 720 times = 720 threads

will I run into memory issues or will the JVM take care of it

I will try and read up my understanding of threads - but if there are some known gotcha's that should be avoided - that would help !

Thanks ,
-anagha
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Threads a garbage-collected like all objects, and the JVM will do that for you. So unless you keep references to all threads around somewhere, there should be no problems.

Since you mention an EJB container, note that the J2EE spec. doesn't allow creating threads in EJBs.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!