File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes J2EE and Multithreading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "J2EE and Multithreading" Watch "J2EE and Multithreading" New topic

J2EE and Multithreading

Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
My System Architect has told me that writing multithreaded code in J2EE apps is generally discouraged. Can anyone tell me why or point me to something to read. I googled but found little onthe topic.

Fun programming etcetera!
Jeremy Hsu
Ranch Hand

Joined: Mar 28, 2005
Posts: 79
It all comes down to the specification of J2EE. The reason is because the J2EE container takes care of the lifecycle of ejb. As a result, EJB does not support multi-threading. If you require mulitthreading in your application, I think you should not be looking at J2EE. I assume you needed mulitthreading because you are coding some server socket, and this is another thing J2EE forbids.
Dave Wingate
Ranch Hand

Joined: Mar 26, 2002
Posts: 262
I had wanted to speed up some code that looked like this:

run query 1
run query 2
run query 3

use results from all queries to do something useful

The pattern above requires that we pay for bandwidth latency for each query we run. I was able to dramatically reduce the time to run all queries by running all queries in background threads and waiting for all to finish. This would have consumed more connections from our pool, but the increase in speed might have made it a reasonable trade off.

So if EJB doesn't support multithreading, am I stuck with the design pattern of running each query in order?
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
You must not spawn threads from within the EJB container to run the queries. But if your queries originate from outside the EJB container, then by all means submit them in separate threads. If you do it this way, the EJB container will probably run each query in a separate thread.

SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Jeremy Hsu
Ranch Hand

Joined: Mar 28, 2005
Posts: 79
Hi, Dave. Yes with EJB you can not do mulit-threading, but it does not mean you have to execute each of them in order. I think you are talking about asynchronous processing of the 3 queries. I think what you can do is have your stateless session beans fires 3 JMS messages to the message queue, and you have to code one message bean to handle each query. If you do this, you can have simultaneous processing of 3 queries. However, it is left to you as an problem to solve how to collect the results from the 3 queries.

Maybe for your particular problem, you better not use EJB.
[ June 14, 2005: Message edited by: Jeremy Hsu ]
Murugesh Shriram

Joined: Mar 31, 2005
Posts: 3
My resultset has morethan ten thousand records. Looping it and dumping in dataobjects is talking hell a lot of time. [say 10 mins]

Used a separate thread that would keep updating my session object which holds an array of data objects. My Pagination would take care of the rest...

Is this above solution recommented? Can we have thread in the server side??? Does this hit back badly in future. currently the response time is less than 7 seconds.

Thanks in Advance,
I agree. Here's the link:
subject: J2EE and Multithreading
jQuery in Action, 3rd edition