Solved

await+signal (wait+notify) servlet threads

Posted on 2010-08-31
3
949 Views
Last Modified: 2012-05-10
Hi

Could/should I use EJB in this scenario?

I have a servlet that implements the answer to this solution (sweets packet answer):
http://www.experts-exchange.com/Programming/Languages/Java/New_to_Java/Q_24924094.html

When I apply the above solution to my servlet I get an IllegalMonitorStateException, although I have an JSE version which works.

I am managing the threads myself in the servlet without the use of EJB.  

To use the above metaphor, the sweets packet (persistent object) is instantiated, then children (client side) would grab sweets over http (entering on servlet thread_1) and the parent would replenish sweets from the database (server side working in daemon thread_2).

In real life a screen extending the Screen class  is instantiated in a servlet (running in thread_1).  The client might make a request like "purchase" to the server.  The purchase method of the screen object is invoked on a second thread (thread_2).  That purchase method might require client input, so it could pause itself and resume the servlet thread to prompt for client input.  When the client input arrives, the servlet thread would pause, waiting for further instructions from the purchase method etc.

Question :
Can I run a statefull session bean (SFSB) in a new thread and pause its execution while an http request completes as below?

An http conversation might look like this:

[http post request] client clicks : "purchase"

[thread_1] servlet invokes purchase method in thread_2 and pauses itself]

[thread_2] purchase method starts, hits database etc. needs client input, pauses itself, resumes thread1

[http post response] servlet message "how many"

[http post request] client enters : "5"

[http post response] servlet message "here is 5 for you"

[thread_1] servlet resumes thread_2 and thread_1 dies

[thread_2] completes and dies

Open in new window

0
Comment
Question by:arch-itect
  • 2
3 Comments
 
LVL 10

Accepted Solution

by:
Hegemon earned 500 total points
ID: 33571730
Without going into details, EJBs are not permitted to mess with Threads: http://java.sun.com/blueprints/qanda/ejb_tier/restrictions.html#threads
0
 
LVL 2

Author Comment

by:arch-itect
ID: 33571825
Thanks Hegemon, that kind of settles it but how does one manage multithreading in a servlet?

From http://java.sun.com/blueprints/qanda/ejb_tier/restrictions.html#threads :
... Multithreaded applications are still possible, but control of multithreading is located in the container, not in the enterprise bean.
0
 
LVL 10

Expert Comment

by:Hegemon
ID: 33572059
- how does one manage multithreading in a servlet ?

Servlet or not -  threads are managed in the same way anywhere. EJBs are exceptions since they are managed by container. Basically you spawn a thread, let it run and probably poll it periodically, checking for some condition. When the condition is met, the parent thread continues, the child thread dies.

- Multithreaded applications are still possible, but control of multithreading is located in the container, not in the enterprise bean.

That's related to my previous comment. If you need to organise a co-operative process involving more than one thread, EJBs are not for you where it comes to organising the process. All manipulations with threads have to be done outside of EJBs. However, you can spawn a thread, it can instantiate an EJB, call its methods and consume the results, but no manipulations with threads inside EJBs.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
get weblogic logged in user in java 2 59
Basic Java Case or If-Else statement... 3 43
replace a word with other 1 40
how to install java on RHEL image on EC2 4 13
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now