Why no thread.sleep in servlet and a light way to poll from a servlet

A

anita

I have a requirement where I have to wait a few seconds-poll- wait a
little more- poll etc., a few times, from a remote host a couple of
times before giving up. All this in a servlet.

Its really tempting to use Thread.sleep. I have seen several
references to "dont use Thread.sleep" in J2EE. But I am planning on
using this in a servlet. I read the servlet spec which doesnt say
anything about not using Thread.sleep.

I know I am not allowed to start my own threads, because the container
maintains them. but I cant think of why I cant use thread.sleep.

What are my options for pausing a few seconds in a servlet thread ?
this is in J2EE1.4
One option is to try to wait on a socket connection with a timeout-
seems silly to waste a socket just to gt a timer facility.

Thanks
A
 
A

anita

Hmm.. I read the spec again- it seems to imply that I CAN start my own
threads in a servlet container.

So maybe a TimerTask will do- or a simple Thread.sleep
I dont need to have it happen periodically or anything.
Just want the incoming request to poll a resource a couple of times
before giving up.

Thanks
A
 
M

Manish Pandit

Hmm.. I read the spec again- it seems to imply that I CAN start my own
threads in a servlet container.

So maybe a TimerTask will do- or a simple Thread.sleep
I dont need to have it happen periodically or anything.
Just want the incoming request to poll a resource a couple of times
before giving up.

Thanks
A

You can spawn threads from a servlet container. I guess you're mixing
J2EE Server, EJB Container and a Servlet Container. It is recommended
not to spawn threads from an EJB as the container manages the thread
pool(s), bean state and transactions which threading can interfere
with.

As far as the Servlet Containers go, they spawn 1 thread per request
which runs a single instance's service() method (Unless the servlet
implements singlethreadmodel, where there will be 1 servlet instance
per thread per request). Anyway, when you make the thread wait, there
is a chance that when multiple requests for the same requests come in,
and the server has n threads waiting, it might run out of threads in
the pool and queue up the requests, eventually timing out if the
threads take forever. Some containers (tomcat) are smart enough to
pool serveral servlet instances. Either way, there is a high chance of
running out of threads from the container pool if the threads block
forever, or the servlet gets way too many requests.

These are just some things to consider - a lot has to do with the
requirements of your app, and the usability.

-cheers,
Manish
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

anita said:
Hmm.. I read the spec again- it seems to imply that I CAN start my own
threads in a servlet container.

So maybe a TimerTask will do- or a simple Thread.sleep
I dont need to have it happen periodically or anything.
Just want the incoming request to poll a resource a couple of times
before giving up.

You are allowed to do it from a servlet.

If it is something only a single or few users are
doing it is also OK.

If many users will be doing it then you can get
problems with the server.

Why not do it another way like:
- have one request send a message to a message queue
- have a MDB process it
- have the client poll at a reasonable interval to a
servlet that checks if the work is done
?

I would think the container would prefer 3*N requests
that runs quick over N requests that each blocks for
seconds.

Arne
 
?

=?windows-1252?Q?Arne_Vajh=F8j?=

Manish said:
It is recommended
not to spawn threads from an EJB as the container manages the thread
pool(s), bean state and transactions which threading can interfere
with.

I think "recommended" is understating.

#• The enterprise bean must not attempt to manage threads. The
enterprise bean must not attempt
#to start, stop, suspend, or resume a thread, or to change a thread’s
priority or name. The enterprise
#bean must not attempt to manage thread groups.

Arne
 
R

Roedy Green

What are my options for pausing a few seconds
in a Servlet thread? This is in J2EE 1.4
The problem is if you sleep,
you are tying up other
transactions that would normally be processed
with that Thread. What you can do instead
is set up a Timer, which creates a new Thread
for all your timed events.
See http://mindprod.com/jgloss/timer.html
 
A

Andreas Leitgeb

Roedy Green said:
The problem is if you sleep, you are tying up other
transactions that would normally be processed
with that Thread.

This pattern of thought appears to me similar to
removing all waste-bins, in the hope that people
would then avoid buying unrecyclable stuff. No,
they'd more likely just litter more.
- and in the java context at hand, they'd probably
rather create a busy loop to waste the time, rather
than refactor their application to use asynchronous
timers, for whose triggering they think they need to
wait.

my opinion: never try to inhibit patterns of behaviour,
whose immediately obvious alternatives you disapprove
even more. (well, unless you also had a way to inhibit
all such alternatives, as well)
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Andreas said:
This pattern of thought appears to me similar to
removing all waste-bins, in the hope that people
would then avoid buying unrecyclable stuff. No,
they'd more likely just litter more.
- and in the java context at hand, they'd probably
rather create a busy loop to waste the time, rather
than refactor their application to use asynchronous
timers, for whose triggering they think they need to
wait.

my opinion: never try to inhibit patterns of behaviour,
whose immediately obvious alternatives you disapprove
even more. (well, unless you also had a way to inhibit
all such alternatives, as well)

I think programmers will learn that busy loops are bad long
time before they learn that sleep is bad in container
managed threads.

Arne
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top