Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

J2EE and Multithreading

 
Dave Wingate
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeremy Hsu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jeremy Hsu
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Problem:
My resultset has morethan ten thousand records. Looping it and dumping in dataobjects is talking hell a lot of time. [say 10 mins]

Solution:
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...

CAN WE HAVE THIS:
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,
Murugesh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic