aspose file tools*
The moose likes JBoss/WildFly and the fly likes Is it advisable to create own thread in JBoss application server? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Products » JBoss/WildFly
Bookmark "Is it advisable to create own thread in JBoss application server?" Watch "Is it advisable to create own thread in JBoss application server?" New topic
Author

Is it advisable to create own thread in JBoss application server?

Santhosh Kalisamy
Ranch Hand

Joined: Apr 15, 2008
Posts: 56
Hi,

My application is running on JBoss server. It is an java webservice application.

1. Receive the request XML.
2. Parse the XML
3. Process the XML
4. Prepare Response XML.
5. Record (Store in database) all the information
6. Return Response XML.

I was planning to introuduce my own thread so Step 5 would be Asynchronous call and Step 6 won't need to wait for Step 5's completion.

But, one of my friend told me that, "it is not advised to create Thread on your own thread inside the App Server".

And recommended to obtain them from the JBOSS pool, and configure it there.

But, He is not sure about the reason. Would it really a good suggesstion? If so, what is the reason?

Can anyone help me to understand please.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2337
    
  28

Generally starting and stopping threads in any server is not advisable, because threads are costly. If you have to do some background processing, it's better to use a thread pool so that a) your don't use kernel resources by starting/stopping threads and b) you don't overwhelm the system by starting too many threads. A thread pool is generally configured with a max limit on number of threads.

Remember that the resources on any system are finite. Logically, programmers tend to think of threads as something akin to a new worker. However, the computer cannot magically create more CPU, or add more memory or make the hard disk spin faster. By adding your own background threads, you are taking resources away from JBOSS. By starting too many threads, you start a feeding frenzy among the threads and cause the threads to fight with each other. Coming up with the right number of threads to start is not easy. It requires a lot of analysis, fine tuning and testing. It's very easy to start new threads , much difficult to do it properly

SO, I wouldn't say don't ever start your own threads. However, I will say don't start threads lightly.
Santhosh Kalisamy
Ranch Hand

Joined: Apr 15, 2008
Posts: 56
Thanks Jay for quick reply.

I have received one suggesstion to go with ThreadPool (Apache commons). I couldn't find any information, how to do it?

Could you please give me the some suggestions to achieve or some sample for Threadpool.

I will dig around more.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2337
    
  28

Apache Thread Pool has a very short and useful example right on their main page

Java's own ExecutorService will do a good job too. There is a sample on the APIDoc


Generally thread pools are built on a Publisher Consumer pattern. Java docs for both implementations are very good, but they assume that you are familiar with the Publisher COnsumer pattern. I would suggest that you read up on that pattern (if you haven't already) before you go into API docs.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

The J2EE standard explicitly forbids request processors from spawning threads.

Request processors should do their work quickly and return a response. For things that cannot be done quickly, set up an independent process in the application startup context listener and use synchronized objects to communicate with it. You can do any threading you like in the context listener-spawned processor.


Customer surveys are for companies who didn't pay proper attention to begin with.
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5823
    
    7

Another possibility is to use messaging - set up a queue and a listener and pass the asynchronous tasks to the queue.


JBoss In Action
 
 
subject: Is it advisable to create own thread in JBoss application server?